SEND FIREBASE NOTIFICATION USING NODE JS TO ANDROID – FURTHER READING
Abstract:
Till now, we have sent push notification of Firebase Cloud Messaging using our Node js server to our Android users.
The notifications were displayed in the android notification stray. And on tapping, the launcher activity was opening by default. In this section we will discuss about how to make the notification take further actions like opening our desired activity or else.
Read With:
SEND FIREBASE CLOUD MESSAGES (FCM) TO YOUR DESIRED ANDROID USERS USING NODE JS SERVER
Also Read Width:
SEND YOUR FIRST ANDROID NOTIFICATION USING FCM (Firebase Cloud Messaging)
We will make few additions to the following modules:
1. In the payload of our Node js web app module.
2. add some code in the manifest file.
3. Create a NotificationView activity with its layout xml file to be opened after tapping the notification.
4. Make few changes in the overridden onMessageReceived method of the FirebaseMessagingService subclass.
Let us go.
1. In our node js module modify the payload:
Add click_action as an attribute under notification
var message = ‘Good Morning’;
var payload = {
notification: {
title: 'Notification from Node js Server',
body: message,
'click_action': 'OPEN_ACTIVITY_1'
},
data: {
'extra': message
}
};
Set a value for the action for ‘click_action’ attribute (say, OPEN_ACTIVITY_1). This action name will be required in the intent-filter of the activity which we want to open on tapping the notification.
Also set the data value with one or more attribute. Here we added an attribute ‘extra’.
We will get the value as the data payload from the intent (through getIntent() method) from the activity which will be opened on tapping the notification.
2. Now we will modify our android manifest file.
Here we have specified OPEN_ACTIVITY_1 as the name of the intent filter of the activity NotificationView.
3. In the NotificationView activity get the data payload as extras from the getIntent() method of the Activity.
We can also get the values with getStringExtra(String) method of getIntent().
So, the data content of the payload will be displayed on tapping the notification in this activity, when the app is in background.
4. Now, if the app is in foreground state, we should make the following changes in the onMessageReceived method of the FirebaseMessagingService subclass.
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
if(remoteMessage.getNotification() != null){
try {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
addNotification(body);
}catch(Exception e){
e.printStackTrace();
}
}
}
In the addNotification method we create the notificationIntent with (again) the NotificationView activity and send the notification payload attribute value as extra (with putExtra(String, String)). This will open the NotificationView activity with data from onMessageReceived method.
Again, don’t forget to add Notification Channel, with notification builder.
Now you can open your desired activity on tapping the notification inyour Android app.