Bills
Bills

Reputation: 798

Open specific fragment destination in Navigation graph when click on push notification & graph is in other than launcher activity

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

Upvotes: 1

Views: 1448

Answers (1)

rmdan
rmdan

Reputation: 123

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()

Upvotes: 4

Related Questions