NavDeepLinkBuilder destination ignored when app not in foreground

拈花ヽ惹草 提交于 2021-02-07 06:51:19

问题


I am using NavDeepLinkBuilder to generate a pending intent for a push notification to open the app at a particular destination.

        return NavDeepLinkBuilder(this)
                .setComponentName(MainActivity::class.java)
                .setGraph(R.navigation.main_navigation)
                .setDestination(destinationId)
                .setArguments(args)
                .createPendingIntent()

When the app is in the foreground, the notification will navigate to the destination set in the pending intent built by the NavDeepLinkBuilder. 👍

However when the app is not in the foreground, the notification will only navigate to the MainActivity and ignore the destination set in the NavDeepLinkBuilder. 👎

Note: The pending intent and notification are built in a service extending FirebaseMessagingService.


回答1:


The notification clicks end up behaving differently when the app was backgrounded.

A user tap on the notification opens the app launcher by default. 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.

See: https://firebase.google.com/docs/cloud-messaging/android/receive#backgrounded

You will end up having to look at your main activities' intent extras to get the notification information. In some cases if you added a click_action to your push notification you will need to add to your intent filter list.

I added the following intent filter actions to my main activities intent-filter block:

        <intent-filter>
            <action android:name=".HomeActivity" />

            <category android:name="android.intent.category.DEFAULT" />

            ... Rest of your actions
        </intent-filter>

This allowed me to then extract the notification information in my HomeActivity



来源:https://stackoverflow.com/questions/58178886/navdeeplinkbuilder-destination-ignored-when-app-not-in-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!