Skip to main content

Manage & Broadcast Local Notifications

Manage & Broadcast Local Notifications

Use this component to broadcast a local notification on your device.

To get started using it, you must use KmpLocalNotifications. All functions are thread safe and main thread safe.

Broadcast a local notification


_1
KmpLocalNotifications.sendNotification("Notification Title", "Notification Message to send to the user")

Broadcast repeated Notifications

To broadcast notifications on an interval, or a loop, please use these functions:


_14
/**
_14
* Schedules an alarm, and broadcasts a local notification with the specified Title & Message, after duration in milliseconds has passed
_14
* @param durationMS milliseconds until the local notification is triggered
_14
* */
_14
_14
KmpLocalNotifications.scheduleAlarmNotification(durationMS: Long, title: String, message: String)
_14
_14
/**
_14
* Schedules a repeating alarm, and broadcasts a local notification with the specified Title & Message, after duration in milliseconds has passed
_14
* @param durationMS milliseconds until the local notification is triggered, and will repeat after every intervalMs set
_14
* @param intervalMs the interval in milliseconds between repeats
_14
* */
_14
_14
KmpLocalNotifications.scheduleAlarmNotificationRepeating(durationMS: Long, intervalMs: Long, title: String, message: String)

Cancelling any Repeating Notifications

If you have any scheduled alarms running on a repeat, please use the below function:


_1
KmpLocalNotifications.cancelAllRepeatingAlarms()

Scheduling a Local Notification

If you need to schedule a local notification at a future date, regardless if the app is running, closed or in the background. Please use the following function:


_2
val scheduleMs = 5000 // duration in milliseconds when the local notification will get triggered
_2
KmpLocalNotifications.scheduleAlarmNotification(scheduleMs, "Notification Title", "Notification Message to send to the user")

Controlling precision of scheduled notifications (Android Only)

Some devices might not support exact broadcasting. Meaning if you schedule a notification to run in 5 seconds from now, the device you are running on might not support a 5 seconds exact broadcast. To control this you can use on your android source set, or android client module.


_1
KmpLocalNotifications.allowSetExact(false) // true to enable precision

The default value for exact is true. Using KmpLocalNotifications.allowSetExact(false) will allow the device to broadcast the notification when its ready (but close to when the duration has expired).

Android Setup

For android please add this into your manifest:


_1
<uses-permission android:name="android.permission.NOTIFICATION" />

And inside your android manifest, please add these permissions:


_3
<uses-permission android:name="android.permission.WAKE_LOCK"/>
_3
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
_3
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/>

You will also need to register a broadcast receiver in your application tag. This receiver handles the scheduling and broadcasting of a local notification once the alarm has been triggered by the device.


_6
<application>
_6
<receiver android:name="com.architect.kmpessentials.localNotifications.receivers.LocalAlarmReceiver"
_6
android:enabled="true"
_6
android:exported="false">
_6
</receiver>
_6
</application>

You will also need to add KmpEssentials into your android.gradle file too, so you can complete the LocalAlarmReceiver registration. However since now you have 2 references to KmpEssentials, it is better practice to migrate this into a common toml file.

In your Multiplatform project you should see a file called libs.version.toml

Add this into your toml file:


_5
[versions]
_5
essentials="1.2.8" // please use the latest package (as of the time of this writing it is 1.2.8)
_5
_5
[libraries]
_5
essentialsApi={ id = "io.github.thearchitect123:kmpEssentials", version.ref="essentials}

Then in both your android.gradle and shared.gradle's commonMain dependencies section add the kmpEssentials reference from your toml file:


_3
dependencies{
_3
implementation(libs.essentialsApi)
_3
}

iOS Setup

None Required