Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Open Composables via notification with Jetpack Navigation

 

 
Igor Escodro
Passionate Android developer
Published: March 31, 2021
Tweet
Share
 
 

https://unsplash.com/photos/f4amnWqRU70

 

Handling screen navigation is a complex task in any framework. Luckily we already have a great tool for helping in this flow, but some different scenarios may need more attention. One of these scenarios is opening the application on a specific Composable in Jetpack Compose.

The Jetpack Navigation Compose is an awesome tool: it uses a solid base from the default Jetpack Navigation for View System with the flexibility needed to handle Composable.

When developing the Navigation Graph in this new library, we create a Composable function similar to this one below, informing the destinations and arguments to navigate in our application. For more information on how to get started with Jetpack Navigation Compose, please access the official docs.

 

 

Back to the View System Jetpack Navigation, when creating PendingIntentfor notifications, the code was similar to:

 

 

However, as we can notice, the code from our Navigation Graph migrated from XML to Kotlin and some of the parameters (like graph and destinations ids) passed to NavDeepLinkBuilder do not make sense anymore.

For more information about PendingIntent, please access the clarifying article by 

Nicole Borrelli

.

 

Deep links to the rescue

The library supports implicit deep links to help the navigation between Composables, associating a specific URL, action and/or mime-type. In order to add this support to our Composable, we add a new parameter to it, deepLinks:

 

 

In the example above, we created a simple URI (https://example.com) and concat a subdirectory (/task_id=) to open the screen in a specific task, based on the id parameter ({task_id}).

Now, we have two alternatives on how to handle the deep link call in our notification.

 

Set the ComponentName to the Activity in the Intent

The first alternative is to set the Activity in the ComponentName when creating the Intent. This will link the URI that we are passing in the Intentwith the Activity containing our Navigation Graph.

 

 

This is a nice approach when the notification and the Activity containing the Navigation Graph are in the same module. If they are separate or do not have the visibility needed, then we have a second alternative.

 

Add an intent-filter to match the deep link

In case the Activity and the notification code are not in the same module, we can add a proper intent-filter to match our deep link declared in the Navigation Graph.

 

 

After declaring the intent-filter in the AndroidManifest.xml, the notification code can create our PendingIntent similar to:

 

 

This time we do not set the Context and the Activity, because the intent-filter will handle this communication for us.

 

How to test the deep links

Deep link needs to be well defined in order to work. If you have some issues (like I did), I suggest using Android Device Bridge to validate if the deep link is correctly set before integrating in your notification, which usually is harder to test multiple times.

In order to test your deep link and intent-filter, run the following command:

 

adb shell am start -W -a android.intent.action.VIEW -d
 “https://example.com/task_id=35”

 

Running this command will open the Composable that we registered earlier with this deep link. If it does not work or open the start destination, probably something needs to be updated.

For more information about deep links in Android, please access the official docs.

 

Final thoughts

It is great to already have a tool as powerful as Jetpack Navigation Compose in the beginning of Jetpack Compose. It allows navigation handling in a familiar way and already has support for ton of features.

I recently updated the notification feature in my personal application to this new approach. So if you want to dig deeper in more real scenario code, please access the pull request below.

 

Update Notification to work with Compose Navigation by igorescodro · Pull Request #154 ·…

…

github.com

 

Thanks a lot for Ian Lake for sharing his thoughts in Slack channel, as well as sharing some great demo for this scenario. ❤️

 

Thanks to Bruno Kenji Tiba. 

 

 

 

Tags: Jetpack Compose, Jetpack Navigation, Android, Android App Development, AndroidDev

 

View original article at: 


 

Originally published: March 26, 2021

Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code 🗣 With Kotlin Scopes
Noisy Code 🗣 With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
Android News
KMM QuickStart Guide
KMM QuickStart Guide

By Mayank Kharbanda

Kotlin Multiplatform (KMP) is a code-sharing technology that allows us to use the same code for different platforms whether it’s JVM, Android, iOS, watchOS, tvOS, Web, Desktop, or WebAssembly. In this article, we will use Kotlin Multiplatform Mobile (KMM) which is a subset of KMP with the focus on providing better tooling and support for sharing code on mobile platforms i.e. Android and iOS.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One