KMPNotifier - Kotlin Multiplatform Push Notification



Simple and easy to use Kotlin Multiplatform Push Notification library (using Firebase Cloud Messaging and Huawei Push Kit) targeting ios and android, and Local Notification targetting android, ios, desktop and web (js and wasm).
This library is used in FindTravelNow production KMP project.
You can check out Documentation for full library api information.

Related Blog Posts
KMPNotifier Update: Web, Desktop, and New Features for Kotlin Multiplatform Notifications
How to implement Push Notifications in Kotlin Multiplatform
Features
- 🔔 Local Notification (android, ios, desktop, js and wasm targets)
- 🔔 Push Notification (Firebase Cloud Messaging & Huawei Push Kit) (android and ios only)
- 📱 Multiplatform (android, iOS, desktop and web (js and wasm))
Installation
Before starting, ensure your project is set up in the respective cloud consoles:
- Firebase: Initialize your project in Firebase, add
google-services.json to your Android app module, and GoogleService-Info.plist to your iOS app.
- Huawei: Initialize your project in Huawei AppGallery Connect and add
agconnect-services.json to your Android app module.
Minimum Requirements
- Android:
minSdkVersion 21
- iOS:
iOS 14.1
Gradle Setup
KMPNotifier is available on Maven Central.
1. Root build.gradle.kts (or settings.gradle.kts):
Add mavenCentral() to repositories and declare the necessary service plugins.
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
plugins {
id() version apply
id() version apply
id() version apply
id() version apply
}
Then in your shared module you add dependency in commonMain. Latest version:
. In iOS framework part export this library as well.
sourceSets {
commonMain.dependencies {
api("io.github.mirzemehdi:kmpnotifier:<latest_library_version>")
}
}
listOf(iosX64(),iosArm64(),iosSimulatorArm64()).forEach { iosTarget ->
iosTarget.binaries.framework {
export("io.github.mirzemehdi:kmpnotifier:<latest_library_version>")
}
}
3. Android App Module (androidApp/build.gradle.kts):
Apply the Android application plugin, the KMPNotifier plugin, and the respective service plugins.
plugins {
id("com.android.application")
id()
id()
}
android {
defaultConfig {
manifestPlaceholders.putAll(
mapOf(
to ,
to
)
)
}
}
Note: If you support both Firebase and Huawei, you will likely need to configure them per flavor. The com.mmk.kmpnotifier plugin aims to simplify this; check its documentation for advanced flavor-specific setups.
Platform Setup
In all platforms on Application Start you need to initialize library using
NotifierManager.initialize(NotificationPlatformConfiguration)
Desktop
Desktop Setup
You need to put notification icon into (or your common resources folder for Compose Multiplatform). For more information:
Web
Web Setup (Js and Wasm)
On application start initialize it using Web configuration
fun main() {
NotifierManager.initialize(
NotificationPlatformConfiguration.Web(
askNotificationPermissionOnStart = ,
notificationIconPath =
)
)
}
Usage
You can send either local or push notification.
Local Notification
Local notifications are supported on Android, iOS, Desktop, JS and Wasm targets. Image is supported on Android and iOS.
Send notification
val notifier = NotifierManager.getLocalNotifier()
notifier.notify {
id = Random.nextInt(0, Int.MAX_VALUE)
title = "Title from KMPNotifier"
body = "Body message from KMPNotifier"
payloadData = mapOf(
Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/",
"extraKey" to "randomValue"
)
image = NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b")
}
Remove notification by Id or all notifications
notifier.remove(notificationId)
notifier.removeAll()
Push Notification
Push notifications are supported for Android (Firebase, Huawei) and iOS (Firebase/APNS).
Listen for push notification token changes
In this method you can send notification token to the server.
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onNewToken(token: String) {
println("onNewToken: $token")
}
})
Receive Notification and Data payload in one callback
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onPushNotificationWithPayloadData(title: String?, body: String?, data: PayloadData) {
println("Push Notification is received: Title: $title and Body: $body and Notification payloadData: $data")
}
})
Receive notification type messages
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onPushNotification(title:String?, body:String?) {
println("Push Notification notification title: $title, body: $body")
}
})
Receive data payload
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onPayloadData(data: PayloadData) {
println("Push Notification payloadData: $data")
}
})
And you need to call below platform-specific functions in order to receive payload data properly when the app is in the background or terminated.
Android
Call NotifierManager.onCreateOrOnNewIntent(intent) on launcher Activity's onCreate and onNewIntent methods.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
NotifierManager.onCreateOrOnNewIntent(intent)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
NotifierManager.onCreateOrOnNewIntent(intent)
}
iOS
Call NotifierManager.shared.onApplicationDidReceiveRemoteNotification(userInfo: userInfo) on application's didReceiveRemoteNotification method.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
NotifierManager.shared.onApplicationDidReceiveRemoteNotification(userInfo: userInfo)
return UIBackgroundFetchResult.newData
}
Detecting notification click and get payload data
Make sure you follow previous step for getting payload data properly.
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onNotificationClicked(data: PayloadData) {
super.onNotificationClicked(data)
println("Notification clicked, Notification payloadData: $data")
}
})
Other functions
val pushNotifier = NotifierManager.getPushNotifier()
For setting custom notification sound, check #61
For setting Intent data in Android (for deeplink), check #60
For permissionUtil, or manually asking notification permission check #27
Logging
If you want to see internal logs of the library, you can set a logger using:
NotifierManager.setLogger { message ->
println(message)
}