FCM 后台通知深层链接不适用于 Android

MON*_*ONK 5 android android-notifications firebase-cloud-messaging

我尝试设置 FCM 通知以使用深层链接。如果应用程序位于前台,那么我们可以通过使用来处理通知FirebaseMessaginService,然后一切正常。当应用程序被终止并且 Firebase 的后台服务显示通知时,问题就出现了。我就是找不到一种方法让深层链接开箱即用。当前的解决方案是将深层链接放在 extras 中,并在应用程序启动时检查意图。但最好跳过这个并让通知传播正确的深层链接。

这是活动清单。深层链接的上升时间为<nav-graph .... />

        <activity
        android:name="com.*****.******.view.main.MainActivity"
        android:exported="true"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">
        <nav-graph android:value="@navigation/services_graph" />
        <nav-graph android:value="@navigation/infobox_graph" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
Run Code Online (Sandbox Code Playgroud)

当然还有服务的设置

        <service
        android:name=".usecase.notifications.OscaPushService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
Run Code Online (Sandbox Code Playgroud)

这对于 FCM 设置来说应该足够了,对吧?(当然按键也设置好了)

现在我尝试使用此有效负载发送深层链接:

                "androidNotification":{
                "click_action": "example://infobox/"
            },
Run Code Online (Sandbox Code Playgroud)

然后点击通知什么也不会发生。ActivityTaskManager将打印以下消息,仅此而已:

I/ActivityTaskManager: START u0 {act=example://infobox/ flg=0x14000000 pkg=com.*****.*****.** (has extras)} from uid 10374 (replaced pkg with stars)
Run Code Online (Sandbox Code Playgroud)

这与使用 adb shell 命令启动深度链接不同。

adb shell am start -d example://infobox/   
Run Code Online (Sandbox Code Playgroud)

会打印

I/ActivityTaskManager: START u0 {dat=citykey://infobox/ flg=0x10000000 cmp=com.****/com.******} from uid 2000
Run Code Online (Sandbox Code Playgroud)

以这种方式启动深层链接实际上是可行的。知道为什么会这样吗?

Fur*_*kul 0

从日志来看,有一个主要区别。有效的有“data”,这是您使用 adb 启动的数据。不起作用的有一个“动作”,它与“数据”不同。您需要在“androidNotification”中传递“data”而不是“click_action”。基本上,“click_action”是用于Intent.setAction(),而您指定的 adb 命令是用于Intent.setData()。这两者是不同的。

无论如何,您可能应该对这些使用数据通知,即使您的应用程序位于前台或被杀死,这也会触发您的服务。然后您可以直接从应用程序正确创建通知。