Send argument through PendingIntent of NavDeepLinkBuilder

后端 未结 0 749
孤独总比滥情好
孤独总比滥情好 2021-02-16 00:40

I\'m having some difficulties sending an argument through a PendingIntent of a notification using NavDeepLinkBuilder. I\'m able to get the destination

相关标签:
回答
  • 2021-02-16 01:23

    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.

    0 讨论(0)
  • 消灭零回复
提交回复
热议问题