How to send push notifications through an Android app using firebase?
A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. Notifications are important to give timely updates to your android app users they require minimal UI to notify the user about the event.
Why Firebase Cloud Messaging (FCM) ?
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost. Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4000 bytes to a client app.
Let’s dive into it’s code
Connect your android app to firebase.
Now add dependencies as given in app level build.gradle file👇👇.
Now make a java class named as FCMReceiver.java this class extends FirebaseMessagingService and it’s overriden method onMessageRecieved is called automatically called when a message comes into the app when the app is whether foreground or background. Here is the FCMReceiver code 👇👇.
Now declare this code into AndroidManifest.xml as given in the code snippet👇👇.
- Now our FCMReceiver is ready now we will move forward to see FCMSender code will we send notification by using the following code👇👇.
- YOUR_ADMIN_SDK_KEY will be replaced by the key given in your firebase project it is located in tab of cloud messaging under project settings see the below screenshot to locate the key
NOTE:- Don’t share the key with anyone else otherwise there will be breach in your application someone having this key can send push notifications to your application so beware.
TIP:- If Breach occurs then click on add server key and delete the older key.
Call the FCMSender.send() method to send notification to the application and you have to give topic to which you wanna send message. If the user is subscribed to the topic then he/she will get notification message. Here we have to pass topic to which we want to send notification. And in “for” we have to send mobile number with country code appended in it, and in “body” you can pass the message you want to send. The message you have to pass while sending notification have format as that of the message string given below👇👇.
Data vs Notification
We have used data in the message because there is reason behind it we also want to show notifications when the application is in background so we have used data but if you have sent notification instead it does not execute the code written in onMessageRecieved so we cannot customize it and it also has one disadvantage it is shown by system so it is shown only when the app is in background i.e app must be in recents otherwise notification will be killed.
Code sample for using Notification as message 👇👇.
Congrats you have learnt how to send notifications through admin sdk firebase in android. Here is small demo of the app that i have made for this blog
For more stuff related to programming follow me. Happy coding🔥🔥😀😀. Till the next blog
Here is the source code to this application https://github.com/swapnil20711/NotificationsAdminSDK