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".
Notification creation:
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)
The context
used in the notification is the FirebaseMessagingService
.
Destination Activity
onCreate()
:
val jobId: String = NewJobDetailsActivityArgs.fromBundle(intent?.extras).jobId
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>
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.
Dependencies: android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha06, android.arch.navigation:navigation-ui-ktx:1.0.0-alpha06
Plugin: androidx.navigation.safeargs
I posted this issue on Google's public issue tracker, and I received the following response:
Source: https://issuetracker.google.com/issues/118964253
The author of that quote recommends using
TaskStackBuilder
instead ofNavDeepLinkBuilder
when creating aPendingIntent
whose destination is anActivity
. Here's what I ended up going with: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 thatActivity
via the generatedDestinationActivityArgs
builder, and accessing the args from the destinationActivity
'sonCreate()
method works.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
orFragment
, or the app is in the foreground and already on the destinationActivity
.addNextIntentWithParentStack()
properly handles up navigation, so clicking the up button from the destinationActivity
navigates back to the logical parentActivity
as defined in theAndroidManifest
.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.