How to listen for an intent using a Broadcast Receiver in a Composable
A step by step guide on listening for an intent, that may or may not have extras, using a Broadcast Receiver in a Composable in Kotlin and Android Studio.
This post is a part of a mini series on sending and receiving functional intents in an Android app. The rest of the tutorials are available at the end.
Please note that we have chosen to use the term "Notifications" to standardize naming conventions across iOS and Android. If you wish to exchange Notifications for Intents, we welcome you too.
Step One: Setup the Notifications Coordinator
For intents to be registered across an app it must be sent from a central manager, which we call a NotificationsCoordinator.
Step Two: Broadcast the Intent
Follow the tutorial below to learn how to structure a project to manage and broadcast intents.
Step Three: Add Disposable Receiver to Composable
Add and customize your Broadcast Receiver using the sample code below.
Composable's need to use a disposable effect so that the Broadcast Receiver is disposed of when the Composable is removed.
To learn more above why we used RECIEVER_NOT_EXPORTED, consult the post below.
Step Four: Add the Composable to the UI
If you have not already, add the Composable to the app.
Step Five: Test
Run the app and verify that the notification is received.
Looking to learn more about Intents in Android ?
Consult the tutorials linked below to learn more about sending and receiving Intents in Android and Kotlin.