2

Currenty working with Navigation UI and FCM and trying to navigate to fragment X when im clicking on notification. Whenever i sending my push notification to my app im creating a peending intent like this

NavDeepLinkBuilder(context)
    .setComponentName(MainActivity::class.java) --tryed with and without this line
    .setGraph(R.navigation.nav_graph)
    .setDestination(R.id.destination)
    .setArguments(bundle)
    .createPendingIntent()

and its working fine when im clikcing on notification when my app is in foreground. But whenever my app is on background pending intent dosent lead my to target destination and start home destination instead.

Also saw similar question but is wasent helpfull.

EDIT: So if you ever faced this behavior in your app here is the solutuion.

When your app in background your onMessageRecived will never triggers, but you will get your notification anyway(just without your intents and construction logic). All you need to do is handle intent in main activity like this

onCreate(){
    //your main activity logic
    //be sure that navController initialized above
    //my notification from FCM has extra key-value pair with key "payload"
    intent?.extras?.get("payload")?.let {
            processBackgroundMessage(it as String)
    }
}

And thats it! Further just implement logic that you need in processBackgroundMessage.

I hope it will be helpfull for anyone

Dwane13
  • 190
  • 1
  • 2
  • 8

0 Answers0