FCM 启动器 activity 并不总是打开

FCM launcher activity not always opened

我在 Android 上使用 Firebase 云消息传递,我 运行 解决了以下问题。当应用程序在后台处理收到的通知时,会发生这种情况。文档说明如下:

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

尽管这是大多数时候发生的情况,但我发现了一些未打开启动器 activity 的极端情况。相反,应用程序只是在后台堆栈的最后 activity 从后台恢复,让我无法获得有效负载。

我每次都可以通过终止进程并通过系统托盘的通知打开应用程序来重现此问题。第一次点击通知时,启动器 activity 会按照文档中的描述打开,但是通过点击任何后续通知,应用程序会简单地恢复,而无需先通过启动器 activity。

我不确定是我做错了什么,还是 Firebase 的 SDK 有错误。您知道为什么会发生这种情况吗?

我正在测试的通知是一个显示消息通知(不是数据通知),它也有一个负载。

{
    "to": "d-3qyvyqefU:APA91bG_nHNYeYuKwB3oIvRKStVgSyelTIhX6rtu6LGPAjgo-LGDVk9iFO5LWZ-XFMeeRVkZm0suMyJAnWORWbZlMaVcHmhFTZOKPI_A-D2wYXM0SIIT8pZZ2g4W55S1bz9YD5y625fv",
    "priority": "high",
    "content_available": true,
    "registration_id": "d-3qyvyqefU:APA91bG_nHNYeYuKwB3oIvRKStVgSyelTIhX6rtu6LGPAjgo-LGDVk9iFO5LWZ-XFMeeRVkZm0suMyJAnWORWbZlMaVcHmhFTZOKPI_A-D2wYXM0SIIT8pZZ2g4W55S1bz9YD5y625fv",
    "collapse_key": "Collapse Key",
    "data": {
        "type": "default",
        "person": "2123435",
        "token": "asdmmio23j4123azde3"
    },
    "notification": {
        "type": "default",
        "title": "Howdie!",
        "body": "Say hi to your new friend!"
    }
}

对于所有仍然面临此问题的人来说,FirebaseMessagingService class 中有一个公开 Intent 对象的方法。您可以覆盖它并执行如下操作:

@Override
protected int zzaa(Intent intent) {
   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   return super.zzaa(intent);
}

上面的代码应该可以解决这个问题,但 Firebase 的人将来很可能会更改访问修饰符 - 如果他们还没有的话。我已经在他们那边开了一张票,并确认他们可以重现这种行为,并会在某个时候修复这个错误。