Office Hours Transcript: 2021-11-30

john joined

hello, john!

 

how can I help you today?

Hello Mark,

I’m trying to open a composable screen via a notification. I have read that you can do so using a deep link, but I don’t know how to do so. Can you help me? This my navhost

 

@Composable
fun Navigation(){

val navController = rememberNavController()
val uri = "excerpts"

NavHost(navController = navController, startDestination = Screen.Home.route ){

    composable(route = Screen.Home.route){ HomeScreen(navController)}

    composable(route = Screen.DayCardFull.route){ DayCardFullScreen()}

    composable(route = Screen.Settings.route){ SettingsScreen() }

}

}

I have not done that, sorry

 

do you happen to be the John who posted a question about this yesterday on Kotlinlang Slack?

Yes, I am.

I do not have any more information on this than what is in that thread, unfortunately

Ummm, would you mind having a quick look at the article? Someone from Google said implied that it was really is, but I’m not familiar with deeplinks…

 

*someone implied that it was really easy

in the scenario from that article, the deeplink is to some specific piece of content, identified by a task_id

 

but, that is specific to that scenario – there is no requirement for there to be an identifier

 

so, AFAIK, instead of deepLinks = listOf(navDeepLink { uriPattern = "$uri/task_id={task_id}" }), you could have deepLinks = listOf(navDeepLink { uriPattern = "$uri" })

 

(or "$uri/dayCardFull" or whatever)

That’s what I wanted to know, thank you.

 

Can "uri" be anything?

the overall value needs to be something that works as a deeplink, from an Android standpoint, which means it should be an https URL to some domain that you control

 

and it will need to go in the <intent-filter> as shown in the article

 

(assuming that something else is creating the notification – if you are creating it yourself, then you could go the ComponentName approach described in that article)

I’m sorry, I’m not sure I understand what an Uri is then. By "domain that you control", do you mean a website? I thought an Uri was could just be local, the same way files have uri’s

"do you mean a website?" – yes

 

"I thought an Uri was could just be local" – for the purposes of a deeplink, no, sorry

 

the primary use cases for deeplinks are for links in Web pages, links in SMS messages, links in emails, etc.

I see, I will have to find another solution then.

now, if you are raising your own notification, and can use the ComponentName approach, then perhaps the 'deeplink' can be an arbitrary string – I have never tried that

 

or, even if it cannot be a truly arbitrary string, you might be able to use a URL that is for some fake domain name or something

the notifaction is sent by a foreground service which is called by an BroadCast receiver, does it mean it’s my own notification?

your own notification would be one that you are displaying from your own code using NotificationManager (or some wrapper around it)

 

where you are the one creating the PendingIntent that ties back to your activity

 

if you do not have control over the PendingIntent, then effectively it is not your notification

I am controlling the PendingIntent. So now what I am trying to do is call a specific composable from that intent, the same way you call a composable using navhost.

in that case, you would use the "Set the ComponentName to the Activity in the Intent" section in that article, and with luck, your 'deeplink' can be an arbitrary string

@Composable
fun Navigation(){

val navController = rememberNavController()
val uri = "settings"

NavHost(navController = navController, startDestination = Screen.Home.route ){

    composable(route = Screen.Home.route){ HomeScreen(navController)}

    composable(route = Screen.DayCardFull.route){ DayCardFullScreen()}

    composable(route = Screen.Settings.route,
        deepLinks = listOf(navDeepLink { uriPattern = "$uri" })){ SettingsScreen() }

}

}

================

        val maIntent = Intent(
            Intent.ACTION_VIEW,
            "settings".toUri(),
            this,
            MainActivity::class.java
        )
 

I tried this, but it just sent me to Scree.Home.route

 

Do I need to add the Uri to intent-filter?

I would not think so, and you cannot do so for a URL of settings, as that is not a valid URL

 

if you examine maIntent, what does "settings".toUri() turn into?

 

it may be that you need to use something that parses correctly as a Uri, rather than "settings"

"settings".toUri().toString() turns to "settings"

 

Do I need somethings that has "https" to parse correctly? (I’d rather avoid it because it’s confusing)

that starts to get into the implementation details of Navigation for Compose – so, I do not know the answer

 

but, it would not be a complete surprise to me if the developers of Navigation for Compose are expecting system-level deeplinks, which would be https URLs

Fake http worked.

Thank you Mark, have a good day.

you too!

john left