当FCM通知消息被点击时,如何获取消息正文?

5

当应用程序在后台时,通知消息是通过通知(Notification)传递的。当通知被点击时,应用程序启动。我该如何获取消息内容?

这个意图是这样的:

    Bundle[{google.sent_time=1470813025421, from=568540028909, 
google.message_id=0:1470813025865549%31bd1c9631bd1c96,
 collapse_key=com.google.firebase.quickstart.fcm}]

意图中没有消息正文,只有消息ID!谢谢!


阅读此帖子,可能会有所帮助 http://stackoverflow.com/questions/34094210/getting-data-from-clicked-notification-in-android - user6698888
@刘世都,你解决了这个问题吗?你在意图中获取到消息正文了吗?我也遇到了同样的问题。 - Md. Sajedul Karim
3个回答

4
抱歉,您想要做的事情目前不可能实现。
当前无法从打开通知时启动的活动中访问(正文、标题、图标等)通知消息的信息。
您可以访问通知消息的数据组件。
一个可能的替代方案是使用 data-message 消息并创建自己的自定义通知和自定义逻辑。
在此处查看 notification-message 与 data-message 的区别: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages PS:如果您能通过 Firebase 支持页面报告功能请求,这将非常有用。这样团队就可以正确地优先考虑未来的功能。 https://firebase.google.com/support/contact/bugs-features/

4

试试这个

  @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // TODO(developer): Handle FCM messages here.
            // If the application is in the foreground handle both data and notification messages here.
            // Also if you intend on generating your own notifications as a result of a received FCM
            // message, here is where that should be initiated. See sendNotification method below.
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "From: " + remoteMessage.getData().get("message"));
    //        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// if you sending data with custom key
 Log.d(TAG, "Notification Message of custom key: " +remoteMessage.getData().get("your key"));
            sendNotification(remoteMessage.getData().get("message"));
        }

1
这将适用于数据消息和应用程序在前台时的通知消息。当应用程序在后台时,它将无法处理通知消息,因为在这种情况下不会调用onMessageReceived()。相反,系统通知区域将显示通知,并单击它将打开应用程序。 - Frank van Puffelen
1
@FrankvanPuffelen,您能否提供一种解决方案,在应用程序在后台运行时该怎么做? - Jagjit Singh
请参考Diego的答案。 - Frank van Puffelen

0
你必须先将信息放入数据字段中,然后将数据加载发送到服务器。
例如,我发送的内容如下:
...
     CURLOPT_POSTFIELDS =>' {
                '.$to.',
                "notification": {
                    "title": "'.$subject.'",
                    "body": "'.$msg.'",
                    "color":"#ff0000",
                    "tag":"'.$tag.'"
                    '.$action.'
                    '.$image.'
                },
                 "data": {
                    "title": "'.$subject.'",
                    "body": "'.$msg.'",
                    "color":"#ff0000",
                    "tag":"'.$tag.'"
                    '.$action.'
                    '.$image.'
                }
            }'
    ...

然后在Android上,您可以使用以下方法恢复数据:
...
Object [] keys = intent.getExtras().keySet().toArray();
        String keysString = "";
        for( int i=0;i<keys.length;i++){
            Log.d("PUSH++", keys[i]+": "+intent.getExtras().get((String)keys[i]).toString()+" i"+i); 
    }
...
Log.d("PUSH++", "title: "+intent.getStringExtra("title"); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接