AndroidPub
Published in

AndroidPub

Firebase Push Notification

So you don’t want your client to check his/her inbox page again and again whether a message has come or not, whether his post has got any like or not. It is embarrassing and also time consuming. A Firebase push notification is very helpful for providing instant notification in mobile apps.

Implementation

Go to Firebase console and login with your Google account

After login, you’ll be moved to the following page

Click on New Project, and fill all necessary information.

After filling information, click on Create Project button. Your new Firebase project will be configured.

From your MyFirebaseDemoApp dashboard, select android option for enabling push notification in android.

In next screen, type your Android project package name and click on Register app. After that you have to download config file which is in JSON format.

Open your project in Project mode instead of Android mode, right click on app and paste google-services.json

We are almost there. Just we need to make some changes in build files to enable Firebase.

Sync your project and Firebase is ready

Click on next and run your project. If your app is connected to firebase, you’ll be notified in the next screen. If not notified, no need to worry. You are done and due to any minor issue it is not reflecting. You can click on skip this step

Sending Firebase Push Notification

For sending push notification, you have to add dependency for cloud messaging as follows

implementation 'com.google.firebase:firebase-messaging:19.0.0'
Go to your project dashboard and click on Send your first message in Cloud Messaging tab

Note: The notification you send from this dashboard will only be seen in mobile only if app is running but in background (App should not be in foreground)

Oh No! How can i get push notification when my app is running or in foreground?

With FCM, you can send two types of messages to clients:

  • Notification messages, sometimes thought of as “display messages.” These are handled by the FCM SDK automatically. These messages trigger the onMessageReceived() callback only when your app is in foreground.
  • Data messages, which are handled by the client app. It can trigger the onMessageReceived() callback even if your app is in foreground/background/killed.

So the best way of sending push notification is sending Data messages. Unfortunately Firebase have not developed a UI to send data-messages to your devices, yet. We can send only Notification Messages from dashboard.

How can I send data-messages type notification to devices so that my client is notified even when his / her app is in foreground/ background/ or even killed?

We can easily do. Firebase provides an API by which we can send data-message type push notification.

Implementation

To this, you have to perform a POST request to the following URL:

https://fcm.googleapis.com/fcm/send

Headers

  • Key: Content-Type, Value: application/json
  • Key: Authorization, Value: key=<your-server-key>

Body to send it to specific devices

{
"data": {
"my_custom_key": "my_custom_value",
"my_custom_key2": true
},
"registration_ids": ["{device-token}","{device2-token}]
}

Here we need two things.

  • your server key
  • FCM device token

From where I can get these two things ????

◘ Getting Server key

We can get server key from our firebase project dashboard

◘ Getting FCM device token

Firebase automatically creates a unique FCM token for every device by following the bellow steps. It will take a little bit time, keep patience.

  • Create a service and extend FirebaseMessagingService. Don’t forget to define this service in your manifest.xml file as
<service
android:name=".MyFirebaseService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
  • Override onNewToken(String s) method with the following code.

After doing this you can get your FCM token anywhere by using these lines

You can store this fcm token to your back-end data base at the time of registration.

♣ Last but not least

We have to show a notification whenever our app gets it. For this we can override onMessageReceived(-) method. In this method just display a notification. You can see the full service class code here

Wow! All is set from device end. The only thing which is currently left is to send a notification from back-end server which may use php, java, python and etc.

“ Now should I go to learn php, java or back-end languages to test a push notification?

I’m an Android Developer and I don’t have any php or java guy to help me. How can i send notification? “

Relax! Let me introduce a wonderful tool called Postman. It is used to call and test web-services api easily. It will help you. You can easily download or add widget in Google Chrome app. What are you waiting for? Go to Google Chrome and open Postman

If you have already opened postman, just call the API as follows

♣ In Request URL, just enter FCM API URL

♣ Click on header tab and fill the data as

♣ Go to Body, choose raw and fill as

Here value for registration_ids attribute is your device FCM token which we’ve discussed how to get.

♥ Finally click on Send button.

Wow! You got a new notification in your mobile.

You can check the full source code here

I hope it will be helpful for someone. Thank you☻☻☻

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store