【问题标题】:Firebase Cloud Messaging - Android: click-action of notification when app is closed doesn't workFirebase Cloud Messaging - Android:应用关闭时通知的点击操作不起作用
【发布时间】:2017-04-05 03:20:23
【问题描述】:

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

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

而且效果很好。当应用在后台时,行为是不同的:

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

而且它似乎不太好用。 我在通知 JSON 中使用了“click_action”参数,在该参数中,我指定了我想要打开的“活动”标签内的 Android 清单中使用的意图过滤器的名称。例如,我的通知可能是:

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

我的清单在“MainActivity”中有这个意图过滤器:

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

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

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

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

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

【问题讨论】:

  • 您好,您有解决方案吗?
  • @Bahu 不幸的是,我找到了一个仅使用数据消息的解决方案,使用系统托盘通知该错误仍然存​​在。我认为这是一个 FCM 错误。您是否尝试过我在问题中描述的相同错误?
  • 是的,兄弟,我正在尝试解决这个问题。如果您对“数据消息,使用系统托盘通知”有任何建议,请告诉我
  • @Bahu 我可以使用我的方法为这个答案编写一个解决方案,但正如我之前所说,在我的解决方案中我只使用数据消息。这意味着您的服务器必须始终发送数据消息,而不是显示消息。仅使用这种类型的消息通知工作正常。如果你想告诉我,我在下面写一个完整的答案。
  • 好的,兄弟,请写下答案。谢谢

标签: android firebase firebase-cloud-messaging


【解决方案1】:

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

通知负载示例:

{
"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),因为它会在 Activity 已创建时处理新的 Intent。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2022-12-10
    • 2019-10-06
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多