通过NavDeepLinkBuilder的PendingIntent发送参数 [英] Send argument through PendingIntent of NavDeepLinkBuilder
问题描述
使用 NavDeepLinkBuilder
通过通知的 PendingIntent
发送参数时,我遇到了一些困难。我可以通过单击通知来启动目标 Activity
,但 Activity
的 Intent
不包含我通过 NavDeepLinkBuilder
传递的参数值。 Intent
会返回我在导航图中设置的 defaultValue
- noJobId。
I'm having some difficulties sending an argument through a PendingIntent
of a notification using NavDeepLinkBuilder
. I'm able to get the destination Activity
to launch by clicking the notification, but the Activity
's Intent
doesn't contain the argument value that I passed it through the NavDeepLinkBuilder
. The Intent
instead returns the defaultValue
that I set in the nav graph - "noJobId".
创建通知:
val notification =
NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id_new_job))
...
.setContentIntent(
NavDeepLinkBuilder(context)
.setComponentName(NewJobDetailsActivity::class.java)
.setGraph(R.navigation.main_graph)
.setDestination(R.id.newJobDetailsActivity)
.setArguments(
NewJobDetailsActivityArgs.Builder()
.setJobId(event.jobId)
.build()
.toBundle()
)
.createPendingIntent()
)
.build()
notificationManager.notify(notificationId, notification)
通知中使用的上下文
是 FirebaseMessagingService
。
目标活动
onCreate()
:
Destination Activity
onCreate()
:
val jobId: String = NewJobDetailsActivityArgs.fromBundle(intent?.extras).jobId
main_graph .xml
导航图:
main_graph.xml
Nav graph:
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_graph"
app:startDestination="@id/jobsFragment">
<fragment
android:id="@+id/jobsFragment"
android:name=".ui.jobs.JobsFragment"
android:label="JobsFragment">
<action
android:id="@+id/action_jobsFragment_to_newJobDetailsActivity"
app:destination="@id/newJobDetailsActivity" />
</fragment>
<fragment
android:id="@+id/historyFragment"
android:name=".ui.history.HistoryFragment"
android:label="HistoryFragment" />
<fragment
android:id="@+id/profileFragment"
android:name=".ui.profile.ProfileFragment"
android:label="ProfileFragment" />
<activity
android:id="@+id/newJobDetailsActivity"
android:name=".ui.job.NewJobDetailsActivity"
android:label="activity_new_job_details"
tools:layout="@layout/activity_new_job_details">
<argument
android:name="jobId"
android:defaultValue="noJobId" // just for testing
app:argType="string" />
</activity>
</navigation>
还有其他人遇到此问题吗?我觉得这是导航组件的错误,但我还不确定100%。
Has anyone else run into this issue? I have a feeling it's a bug with the Navigation component, but I'm not 100% sure yet. Curious if there's something I'm missing here.
依赖项:android.arch.navigation:navigation-fragment-ktx:1.0.0- alpha06,android.arch.navigation:navigation-ui-ktx:1.0.0-alpha06
Dependencies: android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha06, android.arch.navigation:navigation-ui-ktx:1.0.0-alpha06
插件:androidx.navigation.safeargs
Plugin: androidx.navigation.safeargs
推荐答案
我在Google的公共问题跟踪器上发布了此问题,并收到了以下答复:
I posted this issue on Google's public issue tracker, and I received the following response:
NavDeepLinkBuilder将其args传递给NavController,以将
深层链接到特定目标。活动目的地实际上是导航图中的
出口点,而不是可以/应该与
深度链接的东西。
NavDeepLinkBuilder passes its args to a NavController to deep link into a specific destination. Activity destinations are really more exit points from a navigation graph than something that can/should be deep linked to.
来源: https://issuetracker.google.com/issues/118964253
该引用的作者建议使用 TaskStackBuilder
而不是 NavDeepLinkBuilder
创建目的地为活动
的 PendingIntent
时。这就是我最终要做的事情:
The author of that quote recommends using TaskStackBuilder
instead of NavDeepLinkBuilder
when creating a PendingIntent
whose destination is an Activity
. Here's what I ended up going with:
NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id_new_job))
...
.setContentIntent(
TaskStackBuilder.create(context).run {
addNextIntentWithParentStack(Intent(context, DestinationActivity::class.java).apply {
putExtras(DestinationActivityArgs.Builder(jobId).build().toBundle())
})
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
)
.build()
此解决方案使我能够成功地深度链接到目标活动
,同时仍然能够通过生成的 DestinationActivityArgs $ c引用导航图中为该
Activity
定义的args $ c>构建器,并从目标 Activity
的 onCreate()
方法访问args起作用。
This solution allows me to successfully deep link to a destination Activity
while still being able to reference the args defined in the navigation graph for that Activity
via the generated DestinationActivityArgs
builder, and accessing the args from the destination Activity
's onCreate()
method works.
此解决方案还可以正确处理以下情况:应用程序任务不在最近使用的应用程序列表中,而应用程序在前台显示其他活动
或片段
,或者该应用位于前台且已位于目标活动
。 addNextIntentWithParentStack()
正确处理了向上导航,因此,从目标 Activity
的目标位置单击向上按钮,导航回到逻辑父级<在 AndroidManifest
中定义的code>活动。
This solution also correctly handles the cases when the app task is not in the 'recent apps' list, the app is in the foreground showing some other Activity
or Fragment
, or the app is in the foreground and already on the destination Activity
. addNextIntentWithParentStack()
properly handles up navigation, so clicking the up button from the destination Activity
navigates back to the logical parent Activity
as defined in the AndroidManifest
.
遗憾的是,该解决方案没有直接利用导航体系结构库来构建 PendingIntent
,但这似乎是最好的选择。
It's a slight bummer that this solution isn't directly making use of that Navigation Architecture library to build the PendingIntent
, but this feels like the best alternative.
这篇关于通过NavDeepLinkBuilder的PendingIntent发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!