SEND YOUR FIRST ANDROID NOTIFICATION USING FCM(Firebase Cloud Messaging)
SEND YOUR FIRST ANDROID NOTIFICATION USING FCM(Firebase Cloud Messaging)
Abstract: Here I will be discussing about how we can send push notification messages to all users (of an Android app) using Firebase Cloud Messaging (FCM)
In our next topic I will discuss about sending Firebase Cloud messaging (FCM) through our own server.
1. Create an android project in Android Studio
2. Sign in to firebase with google credentials
3. Now go to the firebase console
(i). here create your own firebase project
Follow the subsequent steps. Then add your android app to the firebase clicking the symbol (for android).
(ii). link and register your android app to the project
4. In this process download the google-services.json config file. Put the file in you application’s root directory(just inside your android project’s app folder)
5. Add the firebase android SDK in the project level and app level build.gradle file:
Build.gradle(project leve):
Add the following line into repositories, dependencies and allprojects section.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
Remember to choose right firebase-bom library 'com.google.firebase:firebase-bom:26.6.0', otherwise build process may fail.
The above bom library will work well with Android sdk 21
7. Now sync the build.gradle file in Android Studio.
8. Make a subclass of FirebaseMessagingService
-> here override the method onMessageReceived(RemoteMessage remoteMessage)
-> get the title and body of the notification message from this RemoteMessage object. With remoteMessage.getTitle() get the title, with remoteMessage.getBody() get the notification message.
Below is the code:
9. Implement the Notification utilities in this subclass.
10. Keep your MainActivity unchanged.
11. In the app manifest add the following code inside the application tag:
12. Now build the project and install it in your device.
13. Now again go to Firebase console. open your project. Go to cloud messaging.
-> [We will go to project settings and collect the service accounts credentials when we will use our own server to put logic to the message sent to our clients(users of the said android app)]
14. Click on ‘Send your first message’.
(i)In the window following set the title and notification text
(ii) Now set the target. Choose your app(package name) as user segment
(iii) Click review, then publish
Your message will appear to your user's handset.
Next Read: SEND FIREBASE CLOUD MESSAGES (FCM) TO YOUR DESIRED ANDROID USERS USING NODE JS SERVER
Next Read: SEND FIREBASE NOTIFICATION USING NODE JS TO ANDROID – FURTHER READING