问题
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