Firebase云消息传递 - Android:应用关闭时的通知点击操作无效

时间:2016-11-21 16:40:07

标签: android firebase firebase-cloud-messaging

我正在使用Firebase云消息传递将数据消息传递到我正在开发的应用程序。根据FCM文档,当应用程序处于前台时:

  

客户端应用程序在onMessageReceived()中接收数据消息,并可以相应地处理键值对。

它工作正常。当app处于后台时,行为会有所不同:

  

可以在用于启动活动的Intent中检索数据有效负载。

它似乎不能很好地工作。 我在通知JSON中使用了“click_action”参数,其中我指定了我要打开的“activity”标记内Android Manifest中使用的intent过滤器的名称。例如,我的通知可能是:

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
        "body" : "This is a test",
        "title" : "Test",
        "click_action" : "com.test.click"
    },
    "data" : {
        "key" : "data"
    }
}

我的清单在“MainActivity”中有这个intent-filter:

<intent-filter>
    <action android:name="com.test.click" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

当我的应用程序处于后台并且我启动通知时,一切似乎都运行良好:我单击通知并显示正确的活动,并且数据键位于收到的意图的“额外”中。 我的应用程序关闭时出现问题:当我点击通知时,应用程序正确启动并使用“com.test.click”intent-filter打开活动,但如果我尝试发送另一个通知,则当我点击时它没有任何反应,在重新启动应用程序之前,后台通知不再起作用。

更清楚一点:当我的应用处于前台或后台时,通知效果很好。如果我的应用程序已关闭,则会正确处理第一个通知,但在重新启动应用程序之前,所有其他后台通知都不再有效:如果我点按通知,我的应用会显示上次观看的活动,就好像我从“最近的应用”菜单。如果我关闭并重新启动应用程序,通知将恢复正常工作。

这是Android中的数据消息或FCM问题的错误吗?有没有办法解决这个问题?

我正在使用Nexus 5 API 23进行测试。

1 个答案:

答案 0 :(得分:7)

我将尝试描述它如何处理我的案例

通知有效负载示例:

{
"notification": {
    "body": "This is a test message",
    "title": "Message",
    "click_action" : "OPEN_ACTIVITY_1"
},
"data": {
    "key": "test key"

}

活动中的明显

<intent-filter>
    <action android:name="OPEN_ACTIVITY_1" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

当应用程序处于后台或关闭时,FCM将创建“通知消息”并且onClick将打开正确的活动,然后在应用程序关闭时处理通知,在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());

并在活动中正确处理它我正在覆盖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),因为它在已经创建活动时处理新的意图。