Send argument through PendingIntent of NavDeepLink

2019-05-25 21:17发布

问题:

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

回答1:

I posted this issue on Google's public issue tracker, and I received the following response:

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.

Source: https://issuetracker.google.com/issues/118964253

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()

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.

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.

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.