如何在 Android 上分离 FCM 通知?

How to separate FCM notification on Android?

我有两个不同的通知。
一个是消息,另一个是其他内容的通知。
我想分开通知。
例如,当我收到通知消息并点击它时,它会打开聊天室,而另一个会打开另一个activity。

以下是发送下游消息所需的一些基本属性。

类型字符串 –(可选)[消息的收件人] 该值必须是单个注册令牌、通知密钥或主题。发送到多个主题时不要设置此字段

registration_ids – 类型字符串数组 –(可选)[消息的收件人] 多个注册令牌,最少1个最多1000个。

优先级 – 类型字符串–(可选)[默认正常] 允许值正常和高。

delay_while_idle – 类型 boolean –(可选)[默认值 false] true 表示在设备激活之前不应发送消息。

time_to_live – 键入 JSON 数字 –(可选)[默认值 4 周最大 4 周] 此参数指定如果设备离线,消息应在 FCM 存储中保留多长时间(以秒为单位)

数据 – 输入JSON Object 指定消息负载的自定义 key-value 对。 例如 {“post_id”:”1234″,”post_title”:”博客Post标题”}

在Android中,您可以在onMessageReceived()中接收它作为地图数据…

public class FcmMessageService extends FirebaseMessagingService{
    @Override
    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());

        if(remoteMessage.getNotification()!=null){
        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"}
        }
    }
}

为此,您应该使用 click_action 字段,它允许您指定 Activity 当用户点击通知时要启动的。

因此在您的通知负载中:

click_action: "<intent to launch>"

如果未定义,则 click_action 默认为启动器 Intent/Activity。