Introduction: Why the Migration is Essential?
Google Firebase is deprecating legacy Firebase Cloud Messaging (FCM) APIs, encouraging developers to migrate to the HTTP v1 API. This shift brings improved security, greater control, and enhanced capabilities. If your application relies on FCM APIs for messaging, and your deadline is over, we can reach out to Firebase Cloud support by sending an email asking to extend the deadline for the project. Recently, we have migrated two of our client projects to FCM APIs to the HTTP V1 API.
Deadline: The legacy APIs was deprecated on June 20, 2023, and shutdown begins on July 22, 2024.
Key Benefits of HTTP v1 API
- Enhanced Security: HTTP v1 uses OAuth 2.0, providing better security through access tokens.
- Fine-Grained Message Targeting: HTTP v1 allows more flexibility with custom data payloads and complex targeting rules.
- Batch Sending Support: Send multiple messages in a single request with batch messaging.
- More Control Over Messages: You have greater control over message fields and how they are processed by Firebase and devices.
What’s Changing?
- Authorization: Instead of the legacy API keys, the HTTP v1 API uses OAuth 2.0 tokens, requiring the use of Firebase Admin SDK for authenticating API calls.
- Message Structure: The request and response formats have changed. Messages are now structured into message, notification, and data objects.
- Batch Requests: The ability to send multiple messages in one request, improving efficiency for mass messaging.
Step-by-Step Migration Guide
Here’s a quick guide to migrating your app to the HTTP v1 API:
1. Obtain OAuth 2.0 Credentials
- Go to the Google Cloud Console.
- Enable the Firebase Cloud Messaging API for your project.
- Create or select an existing service account.
- Generate a new private key and save the JSON file containing your service account credentials.
2. Update Your Codebase
- Use Firebase Admin SDK for backend messaging, or manually authenticate using the OAuth 2.0 token.
- Modify your API requests to use the HTTP v1 endpoint:
3. Batch Messages (Optional)
If you’re sending a large number of notifications, leverage the batch messaging feature:
POST https://fcm.googleapis.com/v1/projects/{project_id}/messages:sendBatch
4. Testing and Validation
Before fully deploying, ensure that you thoroughly test the new API to avoid any disruption in your messaging services. Firebase offers a testing dashboard that can be useful.
Common Migration Challenges
- Token Expiry: OAuth tokens expire after an hour. Ensure that your app refreshes tokens before making requests.
- Error Handling: Update your error-handling logic to accommodate the new error messages and statuses provided by the HTTP v1 API.
- Message Format Changes: Ensure that you format your requests according to the HTTP v1 message structure.