How to broadcast an Intent in Kotlin

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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/15/2023 at 14:16
Last Updated on 06/16/2023 at 11:02
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.

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.

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.

How to create an Android Notification Coordinator in Kotlin

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.

How to implement implicit and explicit Android Intents in Kotlin

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.
Android Developer: Send simple data to other apps

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.

How to create an Android Notification Coordinator in KotlinHow to listen for an intent using a Broadcast Receiver in an ActivityHow to listen for an intent using a Broadcast Receiver in a Composable

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