如何从Notification导航到片段?安卓Kotlin



收到通知后,我想打开我的应用程序并导航到Details片段,因为我使用的是jetpack中的导航组件,但我不知道如何实现它?

这是我的通知服务代码

val intent = Intent(this, DetailedFragment::class.java) 
val builder = NotificationCompat.Builder(this, "100")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(rm.data["title"])
.setContentText(rm.data["body"])
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setCustomContentView(nmrv)
.setCustomBigContentView(exrv)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
val manager = NotificationManagerCompat.from(this).notify(100, builder.build())

根据文档:

显式深度链接是使用PendingIntent将用户带到应用程序中特定位置的深度链接的单个实例。例如,您可能会在通知或应用程序小部件中显示一个明确的深度链接。

您可以使用NavDeepLinkBuilder类来构造PendingIntent

val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.android)
.setArguments(args)
.createPendingIntent()

有了PendingIntent,您可以使用setContentIntent():将其附加到通知中

builder.setContentIntent(pendingIntent)

最新更新