我正在使用导航组件,并且当用户点击通知时,我试图触发指向特定目的地的显式深度链接,由 fragment 表示。
根据documentation可以像这样创建待处理的 Intent :
val bundle = bundleOf("id" to "1234")
val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.myDestination)
.setArguments(args)
.createPendingIntent()
其中 nav_graph 定义如下:<fragment
android:id="@+id/myDestination"
android:name="MyFragment">
<argument
android:name="id"
app:argType="string" />
<deepLink app:uri="myApp://myFragment?id={id}" /> // Removing this line it works fine
</fragment>
然后,我会使用 NotificationCompat.Builder
将pendingIntent 用于通知。和:.setContentIntent(pendingIntent)
当我点击通知时,正确的目的地实际上已打开,但值 args.id
将是“null”(不是 null
,而是一个具有“null”值的字符串。在我的 fragment 中,我有private val args by navArgs<MyFragmentArgs>()
...
override fun onCreate(saveInstanceState: Bundle?) {
args.id // The string value is "null".
}
但是,如果我删除 <deepLink>
从 fragment 然后它将起作用。问题是我需要隐式和显式的深层链接。有没有办法同时支持导航组件?
最佳答案
val bundle = bundleOf("id" to "1234")
val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.myDestination)
.setArguments(args)
.createPendingIntent()
你是什么args
变量,因为你声明了 bundle
以上?我已经检查并且所有代码都运行良好。
我的导航图:
<fragment
android:id="@+id/navigation_notifications"
android:name="com.olopa.thefirst.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications">
<deepLink app:uri="sampleapp://noti?id={id}" />
<argument
android:name="id"
app:argType="string"
app:nullable="true" />
通过通知导航时:val bundle = bundleOf("id" to "12")
val pendingIntent = NavDeepLinkBuilder(this)
.setGraph(R.navigation.mobile_navigation)
.setDestination(R.id.navigation_notifications)
.setArguments(bundle)
.createPendingIntent()
结果:或通过链接导航:
navController.navigate(Uri.parse("sampleapp://noti?id=1"))
结果:关于android - 支持导航组件的隐式和显式深度链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68032731/