How to migrate from GCM to FCM

Ask tech team
From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

How to migrate from GCM to FCM

GCM, or Google Cloud Messaging, is a service enabling messaging between servers and client applications. GCM is used for push notifications as well as for message exchange between client apps and servers. FCM, or Firebase Cloud Messaging, is a cross-platform messaging service supported by Google that performs all functions of GCM plus enables message targeting and simplifies message client development. 

Note: As of April 10, 2018, GCM has been deprecated, although Google still supported it. On April 11, 2019, however, the GCM server and client APIs will be removed. We recommend that you migrate your GCM apps to Firebase Cloud Messaging (FCM) that inherits the reliable and scalable GCM infrastructure, plus many new features. See the migration guide to learn more.

This guide contains a step-by-step instruction on migrating your Android app from GCM to FCM.

Creation of a new project in the FCM admin console and its synchronization with your app

1. Open the FCM admin console (URL: https://console.firebase.google.com).
2. Click Add project.

Add project.png

3. Enter the name of your project in the Project name field. In the same window, mark the checkboxes to use the default settings for sharing Google Analytics for Firebase data and accept the related controller-controller terms. Click Create project to proceed.

Name of project.png

4. On the project page, click the Settings icon next to Project Overview and select Project Settings.

Project settings.png

5. On the Settings page, select the Cloud Messaging tab. It contains your messaging server data that you will need to migrate your Android app. Copy the string in the Token field and the Sender ID.

Cloud messaging.png

6. Select the General tab containing the basic information of your project and click the Android icon to add a new Android app to your project.

Android project.png

Now you will need to configure the Android app data to connect it to Firebase.

7. In the Register app section, enter your Android package name and click Register app to register the application with Firebase.

Android register.png

You can find the Android package name in your gradle app file in the applicationId field. Copy the package name and paste into the Android package name field.

Application id gradle.png

8. In the Download config file section, click Download google-services.json and, once the file downloading is complete, follow the instructions to store it in the root directory of your Android project. 

Google services json.png

9. Switch to the Android studio and open the Project section. 

As project.png

10. Move the google-services.json file you just downloaded to the root directory of the app package. Switch back to the FCM console and click Next.

As json.png

11. In the Add Firebase SDK section, copy the dependencies and add them to the gradle file. You will need to modify the build.gradle files both at the Project and App levels.

Fcm gradle.png

12. Switch to the Android studio and add the dependencies you copied to the build.gradle files for the Project and App.
Project build.gradle file:

As root gradle.png

App build.gradle file:

As app gradle.png

13. Switch back to the FCM console and click Next. The console will display the "Checking..." message showing that it checks whether your app has established communication with the server.

Important: In order to complete synchronization, the new app version has to be built/deployed to the device. A couple of attempts might be required. Please make sure that the old version is removed from the device. 

Fcm sync.png

14. Once the synchronization has completed successfully, the console shows the confirmation message.

Fcm synces.png

QuickBlox migration

1. Modify the AndroidManifest.xml file.

			//BEFORE MODIFICAION
        <service
            android:name=".services.gcm.GcmPushListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>		
 
            //AFTER MODIFICATION		
		<service
            android:name=".services.fcm.PushListenerService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
		//BEFORE MODIFICATION
		<service
            android:name="com.quickblox.messages.services.gcm.QBGcmPushInstanceIDService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
 
		//AFTER MODIFICATION
		<service
            android:name="com.quickblox.messages.services.fcm.QBFcmPushInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

2. Modify the PushListenerService (it may have a different name in your project).

			//BEFORE MODIFICATION
public class GcmPushListenerService extends GcmListenerService {
 
    @Override
    public void onMessageReceived(String from, Bundle data) {
        //Your logic
    }
} 
 
			//AFTER MODIFICATION
public class PushListenerService extends QBFcmPushListenerService {
 
    @Override
    protected void sendPushMessage(Map data, String from, String message) {
        super.sendPushMessage(data, from, message);
        //Your logic
    }
}

3. Update the sender_id field with the value you copied from the Sender ID field of the FCM console.

//BEFORE MODIFICATION
<string name="sender_id">324978348829</string>
 
//AFTER MODIFICATION
<string name="sender_id">133608077321</string>

4. Login to your QuickBlox admin console (https://admin.quickblox.com) and select Push notifications.

Qb push dir.png

5. In the top right corner, choose Settings.

Qb push settings.png

6. Expand the Google Cloud Messaging (GSM) API key section.

Web.png

7. Insert the GCM API key for the Development and Production environment. Select Development and enter the Token value you copied from the FCM console. Click Save key.

8. Repeat the same steps for the Production environment. The new API keys will appear in the Environment: development and Environment: production sections.

Qb server key.png

Your Android app migration from GCM to FCM is complete, and now you can enjoy the benefits of the next-generation messaging service. If you have any questions regarding the migration procedure or encounter problems with migrating your app, contact our support engineers for assistance.