Mohamed Elgarnaoui Mohamed Elgarnaoui - 5 months ago 55
Android Question

How to separate FCM notification on Android?

I have two different notification.

One is a message while the other is notification for something else.

I want to separate the notification.

For example, when I receive notification messages and I tap it, it opens the chat room, while the other one opens another activity.


Here are some of the basic properties required to the send downstream messages.

toType String – (Optional) [Recipient of a message] The value must be a single registration token, notification key, or topic. Do not set this field when sending to multiple topics

registration_ids – Type String array – (Optional) [Recipients of a message] Multiple registration tokens, min 1 max 1000.

priority – Type String – (Optional) [ default normal] Allowed values normal and high.

delay_while_idle – Type boolean – (Optional) [default value false] true indicates that the message should not be sent until the device becomes active.

time_to_live – Type JSON number – (Optional) [default value 4 week maximum 4 week] This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline

data – Type JSON Object Specifies the custom key-value pairs of the message’s payload. eg. {“post_id”:”1234″,”post_title”:”A Blog Post Title”}

In Android you can receive it in onMessageReceived() as Map data…

public class FcmMessageService extends FirebaseMessagingService{
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //onMessageReceived will be called when ever you receive new message from server.. (app in background and foreground )
        Log.d("FCM", "From: " + remoteMessage.getFrom());

        Log.d("FCM", "Notification Message Body: " + remoteMessage.getNotification().getBody());

        if(remoteMessage.getData().containsKey("post_id") && remoteMessage.getData().containsKey("post_title")){
            Log.d("Post ID",remoteMessage.getData().get("post_id").toString());
            Log.d("Post Title",remoteMessage.getData().get("post_title").toString());
            // eg. Server Send Structure data:{"post_id":"12345","post_title":"A Blog Post"}