1

I want to open a specific fragment in navigation component graph when user click on a push notification. But I've more than one navigation graphs. The destination fragment I want to navigate is not in the graph of launcher activity.

val pendingIntent = NavDeepLinkBuilder(this)
            .setGraph(R.navigation.nav_home)
            .setDestination(R.id.detailsFragment)
            .setArguments(args)
            .createPendingIntent()

        val notificationBuilder = NotificationCompat.Builder(
            this,
            getChannelIDForOnGoingNotification(this)
        ).setContentTitle(remoteMessage?.data?.get("title"))
            .setSmallIcon(getNotificationIcon())
            .setColor(getNotificationColor())
            .setContentText(remoteMessage?.data?.get("body"))
            .setPriority(NotificationManager.IMPORTANCE_HIGH)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setStyle(bigText)
            .setContentIntent(pendingIntent)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.notify(notificationId, notificationBuilder.build()) 

I'm using the above code to make deep linking using NavDeepLinkBuilder. But R.navigation.nav_home is not in the launcher activity. When I click on notification it simply open the launcher activity, but don't open the target activity & fragment.

How could I open the target activity that has the graph which has my target fragment using Android Navigation component

Bills
  • 768
  • 7
  • 19

1 Answers1

4

You must add

.setComponentName(MainActivity::class.java)

your code should look like this:

    val pendingIntent = NavDeepLinkBuilder(this)
        .setComponentName(MainActivity::class.java) // your destination activity
        .setGraph(R.navigation.nav_home)
        .setDestination(R.id.detailsFragment)
        .setArguments(args)
        .createPendingIntent()
rmdan
  • 123
  • 2
  • 9
  • Can you write a full code like in the notification class we can implement? it would be more understandable for others as well – Eddie Brock Apr 20 '22 at 05:48