Updated 15 December 2016
As you know Google introduced the new FCM (Firebase Cloud Messaging) as an upgrade to GCM, Its not mandatory to update your app to FCM as the GCM is still functional and the update is not affecting it. But
its recommended to upgrade so that you can get the new features. And for the new apps I’ll recommend to use FCM. So lets start migration our existing android app to FCM.
Firstly import your project to Firebase console and provide the package name and SHA-1. Then download the new google-services.json file and replace it with your existing file in your project.
Please note that now you don’t need any kind of permission in your manifest like in GCM so remove all the GCM related permissions from manifest file. In the app level gradle file change the dependency from
1 |
compile "com.google.android.gms:play-services-gcm:8.4.0" |
to
1 |
compile 'com.google.firebase:firebase-messaging:9.0.2' |
And now all the major changes just replace the code in manifest from
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.gcm" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> </intent-filter> </receiver> <service android:name=".MyGcmListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> <service android:name=".RegistrationIntentService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> <service android:name=".MyInstanceIDListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID" /> </intent-filter> </service> |
to the new code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<service android:name=".MyFcmListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name=".MyInstanceIDListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> |
The new MyInstanceIDListenerService.java
1 2 3 4 5 6 7 8 9 10 11 12 |
public class MyInstanceIDListenerService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); SendTokenToServer(refreshedToken); } public void SendTokenToServer(String token) { //YOUR CODE TO SEND TOKEN TO YOUR SERVER } } |
The FCMListenerService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class MyFcmListenerService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Map data = remoteMessage.getData(); String title = data.get("title").toString(); //your key value instead of 'title' String message = data.get("message").toString(); //your key value instead of 'de sendNotification(title, message); } public void sendNotification(String head, String Content){ //code to create a notification } } |
An you are ready to go. All the setup is ready just a little bit change, on the Activity in which you want to create the FCM token in your application just add the code
1 |
sendTokenServer(FirebaseInstanceId.getInstance().getToken()); |
and the function sendTokenToServer(). And FCM is implemented but don’t forgot that now the API-Key and Sender Key is changed so update them on the server side.
You can also test the application notification from Firebase console as you can directly send message from there. For any assistance you can visit the link.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
8 comments
You can use your former method of sending messages for contacting GCM users and FCM for new ones (but be sure of the API_key you are using) for 2- 3 updates after that most of your users must have been updated the app. Then you can completely move on to FCM console.
https://uploads.disquscdn.com/images/b5115819254f5a3c143b407854a3704f8301919076a55595ed59c1339e5bf560.png
Please check the last line.
Will It work correctly?