Firebase Cloud Messaging (FCM) is the new Cloud Messaging Service by Google for Android, IOS, and Chrome (Web). As Google says FCM is the new version of GCM don’t expect further development on GCM so use FCM in your new projects to benefit extra features for now and future. Don’t get confuse with Firebase pricing FCM is absolutely free.
Benefits of upgrading to FCM SDK include: (Quoted from FCM faq)
– Simpler client development. You no longer have to write your own registration or subscription retry logic.
– An out-of-the-box notification solution. You can use Firebase Notifications, a serverless notifications solution with a web console that lets anyone send notifications to target specific audiences based on Firebase Analytics insights.
OK… Lets start
– Android 2.3 or newer
– Google Play services 9.0.2 or newer
– Google Play services SDK from the Android SDK Manager
– Android Studio 1.5 or higher
Create a Firebase Project at Firebase Console
1. Go to Firebase console and login with your Gmail id.
2. Click CREATE NEW PROJECT, When prompted click CREATE PROJECT after filling your Project name and Country.
3. Click Add Firebase to your Android App
4. When prompted, enter your app’s package name and click ADD APP
(By clicking ADD APP button config file named google-services.json will be downloaded)
5. Copy the downloaded google-services.json file to your projects app directory. then click CONTINUE.
Add Firebase in Gradle
Add Google services plugin to your project level build.gradle to load the google-services.json configuration. then apply it in your app level build.gradle
1. Project-level build.gradle (/build.gradle):
2. App-level build.gradle (//build.gradle):
Click FINISH on the firebase console prompt to complete the setup.
Don’t forget to press Sync to apply the gradle changes…
That’s it now you successfully completed Firebase SDK setup. Now you need to add the required firebase service library to your app level build.gradle.
These libraries are available for the different Firebase features.
We need Firebase Cloud Messaging so add the plug8in for Cloud Messaging / Notifications to your app level build.gradle
Finally click Sync now to load the library… Thats it … let’s move in to the coding part.
To fix Failed to resolve: com.google.firebase:firebase-messaging:9.0.2 error … update your Google Play Services and Google Repository to Rev. 30 and 28 respectively.
For better project structure we keep the FCM related classes in a separate package named fcm. Create a new package and name it fcm.
1. Create a class named FcmInstanceIdService extend FirebaseInstanceIdService then Override onTockenRefresh to get the FirebaseInstance token.
It handles the creation, rotation, and updating of registration tokens. onTockenRefresh will called only on apps first startup and on the next token update… so save the token in the preference for later use.
2. Create a class named FcmMessageService extend FirebaseMessagingService then Override onMessageReceived to handle the received message.
It handles receiving notifications, receive data payload, to send upstream messages, etc.
3. Add both services to the manifest
Send Message from firebase console
1.Go to firebase console, login with your gmail account.
2. Open your project (We created before) … then click Notifications from the side panel.
3. In the Notifications page… Click New Message
4. In the compose form enter Message text and chose the appropriate target…
User Segment – Target a single or multiple app
Topic – Target a Topic of the app
Single device – Target a single device (Enter FCM registration token generated in onTockenRefresh)
5. Finally, click SEND MESSAGE and SEND if prompts.
That’s it for Android Section … Here is the PHP Server Side Implementation to Send FCM Messages to Android Device.