11

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.

oznecro
  • 457
  • 5
  • 16
  • I'm seeing the same behavior. I can't figure out if this a navigation component error or something in the notification. – Marco RS Nov 23 '19 at 01:21
  • @YellowJ Sorry I can't 100% recall my solution and I don't have access to the code anymore. But I remember it had something to do with Activity LauchMode attributes. If you are facing similar issues, maybe you can try looking into this? Sorry for not being too helpful. – oznecro Sep 05 '20 at 08:54

2 Answers2

1

Yes, if you using firebase you can face this behavior. The problem is that your onMessageRecived() method never triggers when your app in the background. In these cases you will receive your firebase message not in onMessageRecived() but in MainActivity() instead. Just check your intent in onCreate() method as I described in my own question here in edit block

Dwane13
  • 190
  • 1
  • 2
  • 8
0

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

Marco RS
  • 8,145
  • 3
  • 37
  • 45