How to broadcast an Intent in Kotlin

Oscar de la Hera Gomez
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “Broadcast Intents.”

A step by step tutorial on sending notifications (intents) with or without extras that can be received by activities or composables within your Android app.

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.

Step One: Setup the Notification Coordinator

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

Follow the tutorial below to setup the manager in charge of sending intents.

Step Two: Create the Intents Model

A screenshot of Android Studio show how we model intents.

In your project, create a Kotlin file that holds an object to keep track of your apps intents.

We suggest calling it SystemNotifications.kt and placing it within a notifications package within the models package.

Step Three: Create the Extras Model

A screenshot of Android Studio showing how we model Extras.

In your project, create a Kotlin file that holds an object to keep track of your apps intents.

We suggest calling it SystemNotificationsExtras.kt and placing it within a notifications package within the models package.

Step Four: Send an internal intent

A screenshot of Android Studio showing how to send an internal intent and one with extras. Code available below.

Internal Intents are explicit intents that are only to be received by your app, the critical aspect here is making sure that the setPackage string is set to match the appId in your projects app level build.gradle.kts.

Please note that we have made the appId a constant called kAppBundleId, which we suggest you add to your project under models > Constants.kt.

To send your first internal intent, in NotificationCoordinator+InternalIntents.kt add your intent using code similar to the one below.

If you wish to use an extra, it will look similar to the one below.

Step Five: Send an External Intent

A screenshot of the Android Developer documentation on a page titled "Send simple data to other apps."
Follow the guide below to learn how to create intents that are sent to other apps. We recommend that you add these to the NotificationCoordinator+ExternalIntents.kt file.

Step Six: Send

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

Anywhere in your app, whether an Activity or a Composable, send a notification by calling one of the functions declared in Step Five or Six.

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.

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.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details