我知道以下内容。 1. FCM 有效负载出现在 onMessageReceived(( 中,当应用程序是前台时。 2.当应用程序在后台运行时,有效负载出现在意图附加功能中。但仅当用户单击通知时。如果用户直接在应用中跳转而不与通知交互,该怎么办? 在这种情况下如何处理有效负载。
我正在发送的有效载荷。
{
"registration_ids" : ["FCM Token Here"],
"notification" : {
"body" : "Testing FCM Message Handling",
"title": "This is a test to handle FCM Messages inside the App"
},
"data" : {
"body" : "Testing FCM Message Handling",
"title": "This is a test to handle FCM Messages inside the App",
"sender_member_id" : "63",
"sender_name" : "Chiranjeev Jain"
}
}
onMessageReceived(( 实现。
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//Here notification is recieved from server
try {
// TODO: Handle Incoming Message
String fcmMessageTitle = remoteMessage.getData().get("title");
String fcmMessageBody = remoteMessage.getData().get("body");
String fcmSenderMemberID = remoteMessage.getData().get("sender_member_id");
String fcmSenderName = remoteMessage.getData().get("sender_name");
Toast.makeText(this, fcmMessageTitle + "n" + fcmMessageBody + "n" + fcmSenderMemberID + "n" + fcmSenderName, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
还将有效负载存储在本地存储中(sqlite,SharedPreferences,无论您喜欢什么(,在应用程序启动时查询该存储,并根据那里找到的内容做出反应。
如果您想在应用程序处于后台时获得回调onMessageReceived()
,则仅发送有效负载。从json
结构中移除通知
{
"registration_ids" : ["FCM Token Here"],
"data" : {
"body" : "Testing FCM Message Handling",
"title": "This is a test to handle FCM Messages inside the App",
"sender_member_id" : "63",
"sender_name" : "Chiranjeev Jain"
}
}