我正在使用 Firebase Cloud Messaging 将数据消息传送到我正在开发的应用程序。根据 FCM 文档,当应用程序处于前台时:
A client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.
而且效果很好。当应用程序在后台时,行为是不同的:
the data payload can be retrieved in the Intent used to launch your activity.
而且它似乎不太好用。 我在通知 JSON 中使用“click_action”参数,我在其中指定要打开的“Activity ”标签内的 Android list 中使用的 Intent 过滤器的名称。例如,我的通知可能是:
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "This is a test",
"title" : "Test",
"click_action" : "com.test.click"
},
"data" : {
"key" : "data"
}
}
我的 list 在“MainActivity”中有这个 intent-filter:
<intent-filter>
<action android:name="com.test.click" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
当我的应用程序处于后台并启动通知时,一切似乎都运行良好:我单击通知并显示正确的 Activity ,并且数据键位于收到的 Intent 的“附加项”中。 当我的应用程序关闭时出现问题:当我点击通知时,应用程序正确启动并使用“com.test.click” Intent 过滤器打开 Activity ,但是如果我尝试发送另一个通知,当我点击它没有任何反应,并且在我重新启动应用程序之前后台通知不再起作用。
更清楚一点:当我的应用程序处于前台或后台时,通知都能正常工作。如果我的应用程序已关闭,第一个通知会得到正确处理,但在我重新启动应用程序之前,其他所有后台通知都不再有效:如果我点击通知,我的应用程序会显示上次观看的 Activity ,就好像我从中选择了应用程序一样“最近的应用程序”菜单。如果我关闭并重新启动该应用程序,通知将恢复正常工作。
这是 Android 中数据消息的错误,还是 FCM 问题?有办法解决这个问题吗?
我正在使用 Nexus 5 API 23 进行测试。
最佳答案
我将尝试描述它如何处理我的案例
通知负载示例:
{
"notification": {
"body": "This is a test message",
"title": "Message",
"click_action" : "OPEN_ACTIVITY_1"
},
"data": {
"key": "test key"
}
Activity 中的 list
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
当应用程序处于后台或关闭时,FCM 将创建一个“通知消息”,然后单击它会打开正确的 Activity ,然后在应用程序关闭时处理通知,在 onCreate() 方法中调用 getIntent()
例如。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hub);
handleIntents(getIntent()); //for simplicity I'm passing the value forward
}
当应用程序位于前台时,我在 FirebaseMessagingService 的 onMessageReceived 方法中创建自定义通知。
例如。
Intent intent = new Intent(this, HubActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("url", remoteMessage.getData().get("url"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Message")
.setContentText("This is a test message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
为了在 Activity 中适本地处理它,我重写了 onNewIntent(Intent intent) 方法。
例如。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntents(intent); //for simplicity I'm passing the value foward
}
所以我认为您缺少的是 onNewIntent(Intent intent),因为它会在 Activity 已创建时处理新的 Intent 。
关于android - Firebase 云消息传递 - Android : click-action of notification when app is closed doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725101/