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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/15/2023 at 16:32
Last Updated on 06/16/2023 at 11:07
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “Receive Broadcasts in Composables.”

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.

SubscribeDownload Open Source Kotlin Starter Project

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

A screenshot of Android Studio showing the NotificationCoordinator.kt file. Code available below.

For intents to be registered across an app it must be sent from a central manager, which we call a NotificationsCoordinator.

How to create an Android Notification Coordinator in Kotlin

Step Two: Broadcast the Intent

A screenshot of Android Studio showing the MainActivity sending an Intent that we declared in this tutorial in the onResume function.

Follow the tutorial below to learn how to structure a project to manage and broadcast intents.

How to broadcast an Intent in Kotlin

Step Three: Add Disposable Receiver to Composable

A screenshot of Android Studio showing the sample Composable  with a disposable effect that incorporates a broadcast receiver.

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.

How to manage what broadcasts Broadcast Receivers recieve in Kotlin

Step Four: Add the Composable to the UI

A screenshot of Android Studio showing us adding the composable to the UI.

If you have not already, add the Composable to the app.

Step Five: Test

A screenshot of Android Studio showing the logs confirming that the composable received the broadcast.

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.

How to create an Android Notification Coordinator in KotlinHow to broadcast an Intent in KotlinHow to listen for an intent using a Broadcast Receiver in an Activity

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe