问题描述
当用户单击推送通知时,我想在导航组件图中打开特定片段。 但是我不止一个导航图。我要浏览的目标片段不在启动器活动图中。
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())
我正在使用上面的代码使用NavDeepLinkBuilder
进行深层链接。但是R.navigation.nav_home
不在启动器活动中。当我单击通知时,它只是打开启动器活动,而不打开目标活动和片段。
如何使用Android导航组件打开包含具有目标片段的图形的目标活动
解决方法
您必须添加
.setComponentName(MainActivity::class.java)
您的代码应如下所示:
val pendingIntent = NavDeepLinkBuilder(this)
.setComponentName(MainActivity::class.java) // your destination activity
.setGraph(R.navigation.nav_home)
.setDestination(R.id.detailsFragment)
.setArguments(args)
.createPendingIntent()