如果您想打开应用程序并在后台执行特定操作,请在通知负载中设置 click_action 并将其映射到您要启动的 Activity 中的意图过滤器。例如,将 click_action 设置为 OPEN_ACTIVITY_1 以触发如下 Intent 过滤器:
正如 FCM 文档中所建议的,要求后端以如下形式发送 JSON 数据,
{
"to": "some_device_token",
"content_available": true,
"notification": {
"title": "hello",
"body": "yo",
"click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
},
"data": {
"extra": "juice"
}
}
并在您的清单文件中为您的活动添加意图过滤器,如下所示
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
当您单击通知时,它将打开应用程序并直接转到您在 click_action 中定义的活动,在本例中为“OPEN_ACTIVTY_1”。在该活动中,您可以通过以下方式获取数据:
Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");
查看以下链接以获得更多帮助:
Firebase FCM 通知 click_action 有效负载 https://stackoverflow.com/questions/37407366/firebase-fcm-notifications-click-action-payload
当应用程序在后台运行时,Firebase onMessageReceived 不会被调用 https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background
Firebase 控制台:如何指定通知的 click_action https://stackoverflow.com/questions/38111339/firebase-console-how-to-specify-click-action-for-notifications