5

I'm trying to open a fragment with args from my notification. Actually, in my case, I have Audio Player running with Foreground Service with Notification and now I want to navigate to my fragment for that specific Audio by passing Audio Id when a user clicks to the Notification.

Riajul
  • 1,140
  • 15
  • 20

1 Answers1

12

You can open your activity with PendingIntent then open your fragment via handling the Intent inside of your activity.

or this answer from similar topic

NavDeepLinkBuilder:

val pendingIntent = NavDeepLinkBuilder(context)
                     .setComponentName(YourActivity::class.java)
                     .setGraph(R.navigation.your_nav_graph)
                     .setDestination(R.id.your_destination)
                     .setArguments(bundle)
                     .createPendingIntent()

//then

notificationBuilder.setContentIntent(pendingIntent)
Beyazid
  • 1,795
  • 1
  • 15
  • 28
  • this is clumsy when having much navigation. Didn't Navigation Architecture guys make any Utils for this? I mean handling Navigation with Intent easier. – Riajul Apr 29 '19 at 18:20
  • You can look at this post. https://stackoverflow.com/a/55245317/11181035 – Beyazid Apr 29 '19 at 18:21
  • Oh did it! Thanks that should be the answer to my question. – Riajul Apr 29 '19 at 18:25
  • 1
    This was really really fast answer, you guys are awesome... thanks again. – Riajul Apr 29 '19 at 18:29
  • This recreates my fragment. Is there a way to have my retained fragment spawned instead? – mhashim6 Jul 07 '19 at 20:26
  • I am not able to recreate the backstack with this soultion. I have Fragment A -> B -> C -> D -> E when I use this solution the graph is only A -> E . Any idea? – AndroidRuntimeException Dec 05 '19 at 22:11
  • Unable to start activity, inside my fragment its showing null pointer exception – Abraham Mathew Mar 26 '20 at 07:55
  • I used NavDeepLinkBuilder within my service and pass the service context and It didn't work can you tell how you used NavDeepLinkBuilder in your foreground service. – Himanshu Malik Nov 04 '20 at 08:43
  • When targeting the new Android S, you have to use `.createTaskStackBuilder() .getPendingIntent(0, FLAG_IMMUTABLE)` instead of `.createPendingIntent()` otherwise your app will crash due to the new Pending Intent restrictions. – Tim May 29 '21 at 12:27
  • How do you create a bundle to pass as an argument? How do you get data from notifications? – krupa parekh Jul 28 '22 at 08:55