Search code examples
androidkotlinpush-notificationnavigationandroid-jetpack-compose

How to navigate to a composable screen from a notification with deeplinks?


I want to navigate to a composable screen when clicking on a notification that contains data like title, body, and an id. This id should be passed to the composable when the route is created.

object Screen : AppDestination("screen/{id}") {
fun createRoute(id: String): String {
    return "screen/$id"
}

This is my composable screen navigation

fun NavGraphBuilder.screenGraph(
    navController: NavHostController
) {
    composable(
        route = Screen.route,
              arguments = listOf(navArgument("id") { type = NavType.StringType },
        navArgument("idnotation") { type = NavType.StringType }),
    deepLinks = listOf(
        navDeepLink {
            uriPattern = "myapp://screennavigation/idnotation={idnotation}"
        }
    ) {
        Screen(
            payloadId = it.arguments?.getString("id"),
        )
    }
}

The "id" is the when a certain item from a list is clicked to navigate to it.

The "idNotation" is the id that i get from the notification.

How can I pass the idNotation to the route if i'm getting it in the deeplink after the route is createRoute is called?

I tested how i'm passing the Id to the intent inside a pending intent for a notification and that works, i'm getting it correctly, I'm just not sure how to pass it to the route.


Solution

  • Your deep link is

    "myapp://screennavigation/idnotation={idnotation}"
    

    Which means that the value of the {idnotation} placeholder will be put into your arguments with the key idnotation. If you want it to instead fill in the id field (to match your route and arguments list), then you'll want to use {id}:

    uriPattern = "myapp://screennavigation/idnotation={id}"