Migrate GCM To FCM

Tram Ho

You can migrate an existing Android app using GCM to Firebase Cloud Messaging (FCM) with the instructions below. Before you get started, there are a few things to remember:

  • GCM and FCM SDKs cannot co-exist on the same application.
  • GCM tokens obtained through GoogleCloudMessaging.register () or InstanceID.getToken () can be used on FCM without changes or refreshes.

1. Import your GCM project as a Firebase project

The instructions below apply when you are trying to add Firebase to an existing Google Cloud project for GCM.

  1. In the Firebase console, select Add Project .
  2. Select your GCM project from the list of available Google Cloud projects, then select Add Firebase .
  3. In the “Firebase welcome” screen, select Add Firebase to your Android App .
  4. Enter the package and SHA-1 name, and then select Add App . A new google-services.json file for your Firebase will be downloaded. Copy that file to your project. Usually in /appp .
  5. Select Continue and continue with detailed instructions to add “Google Services plugin” in “Android Studio”
  • Note: If Google creates an additional server key for your friend, usually with an auto label created by Google Service, “use this new key instead of the old one.

2. Convert to FCM in app-level build.gradle

Before:

After:

3. Edit the manifest file

The FCM SDK automatically adds all necessary permissions as well as functionality upon receipt of the request. Make sure to remove obsolete elements (because they can cause duplication) from the project manifest. Remove from AndroidManifest.xml

4. Update server endpoints

Update the server code to use the new FCM endpoints, to be able to send messages via HTTPP and XMPPP. Note that the new version of gcm-http.googleapis.com/gcm/ is fcm.googleapis.com/fcm/ (without “http”)

FCM supports the same HTTP and XMPP protocols as those of GCM, so you don’t need to update the messaging logic.

* Note: Equivalent to FCM, the GCM HTTP protocol is only labeled “legacy” to clearly distinguish it from the HTTP v1 API. The API is fully supported and Google temporarily has no plans to remove it.

5. Next steps and optional migration tasks

Depending on which GCM feature your application uses, you may have additional migration tasks to perform. Most applications may need to migrate InstanceIDListenerService to work with FCM.

  • Usually required: migrate InstanceIDListenerService Perform this step if you need to access the device registration token because:
  • You send messages to specific devices.
  • You send messages to device groups.
  • You register equipment for topics with the registration management API.
  • Optional: migrate GcmListenerService Perform this step if you need to handle incoming messages, such as when:
  • Your application receives messages with data only.
  • Your application receives notification payloads when it is in foreground .
  • Your application receives an error in case of an upstream message.
  • Optional: update the use of GcmPubSub Perform this step if: If you need to register a new device for the topic. Devices that have subscribed to a topic before the migration will continue to receive messages. For example, a device with topics/news subscribed in GCM will continue to receive messages from /news after migrating to FCM (note, the “topics” prefix is ​​not required in FCM).
  • Special case: Migrate GCM to an existing Firebase project Do this if: You already have a Firebase project and you want to migrate the GCM application and its users to this Firebase project. This situation requires many additional tasks.

6. Config FCM with AWS SNS

FCM is backward compatible with GCM. The steps to set up FCM on AWS are identical to the GCM setup process and (at least for the moment). However, if you are sending data payloads to Android devices, they will not be processed unless you deploy the FirebaseMessagingService client-side service extension. The default JSON message generator in the AWS dashboard sends data messages, which your application will ignore unless the aforementioned service is implemented. To solve this problem, you can provide a custom notification payload that your device will receive (as long as your application is not in the foreground).

The steps you need to take to test GCM / FCM on your application with SNS are:

  1. Create a Platform Application on SNS, select Google Cloud Messaging (GCM) for Push Notification Platform and enter the Server API key into the API key field.
  2. Select Platform Application and click “Create platform endpoint”.
  3. Enter the InstanceID (Device Token) created from your application. You must extend FirebaseInstanceIDService and override the onTokenRefresh method to see this in your Android Application.
  4. Click the “Add endpoint” button.
  5. Click on the ARN link of your application platform.
  6. Select the latest endpoint just created and click the “Publish to endpoint” button.
  7. Select the JSON Message format, and click the “JSON message generator” button.
  8. Enter the text of the message and click the “Generate JSON” button. The message generated by SNS will look like:

As we mentioned earlier, data payloads will be ignored if no service receives them. If you want to test without writing too much code, you should send a notification payload .

  1. Send a message. Before sending, make sure your application is not running on the device and click the Publish Message button. You will then see the notification popup appear on your device.

Refer

https://developers.google.com/cloud-messaging/android/android-migrate-fcm https://stackoverflow.com/questions/38300450/fcm-with-aws-sns

Share the news now

Source : Viblo