In this Firebase Cloud Messaging tutorial, we will see how to send push notification to android device using Firebase Cloud Messaging. So let’s begin.
Why FCM over GCM?
Previously Google has its messaging service named GCM (Google Cloud Messaging). But then there are some grey area reasons to switch to FCM.
1. Google is investing in Firebase, making it its unified mobile platform. Over the last few years, Google have expanded GCM to send messages to multiple platforms beyond Android: iOS and Chrome. Firebase is known for being cross platform, so FCM now makes a natural fit in the Firebase suite of features designed for Android, iOS, and mobile web.
2. With FCM we have Firebase console available to send push notifications on a click; in addition to FCM sever APIs that can be called from backend server to send push notifications.
3. The registration token lifecycle is very much simplified with FCM.
So now we have good reason to learn FCM, let’s start configuring FCM for your project. This is one-time process.
Step 1 : Create simple Android project in Android studio.
Step 2 : Get your package name from AndroidManifest.xml. Mention user permission for Internet access in AndroidManifest.xml file.
Step 3 : Go to firebase console and create a new project.
Step 4 : Window will appear as below. Give a project a name, select your country and click on “CREATE PROJECT” BUTTON
Step 5 : Now click on Add Firebase to your Android App.
Step 6 : Mention package name that you got from step 2.
Click on “ADD APP” button. google-services.json file will get downloaded; add to your project as shown below.
Click on “CONTINUE” button. Follow step as shown in figure and click FINISH.
Now we are ready with setup. Lets start with Implementing Firebase Cloud Messaging
Create a class “MyAndroidFirebaseMessagingService.java” extending “FirebaseMessagingService”
In above code we have created PendingIntent for opening TargetActivity when user clicks on received notification.
Now we have to define the above service in our AndroidManifest.xml file. So go to manifest and modify as follows.
Complete AndroidManifest.xml is as below:
That’s all. Your app is ready to receive notifications.
Sending Push Notification using Firebase Console
• Go to firebase console and select the app you created.
• From the left menu select notification.
• Click on new message.
• Set the message, select your application package from dropdown as shown in below.
• Click SEND MESAAGE.
Those who have installed your application will get the push notification.
Download Complete Project here