Simple and easy to use Kotlin Multiplatform Push Notification library (using Firebase Cloud Messaging) 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
Before starting you need to setup basic setup using Firebase official guideline (like initializing project in Firebase, adding google-services.json
to android, GoogleService-Info.plist
to iOS).
minSdkVersion 21
iOS 14.1
KMPNotifier is available on Maven Central. In your root project build.gradle.kts
file (or settings.gradle
file) add mavenCentral()
to repositories, and add google-services
plugin to plugins.
plugins {
id("com.android.application") version "8.1.3" apply false
id("org.jetbrains.kotlin.multiplatform") version "1.9.20" apply false
id("com.google.gms.google-services") version "4.4.0" apply false
}
repositories {
mavenCentral()
}
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:<version>") // in iOS export this library
}
}
listOf(iosX64(),iosArm64(),iosSimulatorArm64()).forEach { iosTarget ->
iosTarget.binaries.framework {
export("io.github.mirzemehdi:kmpnotifier:<version>")
...
}
}
And in androidApp build.gradle.kts
file you apply google-services
plugin
plugins {
id("com.android.application")
id("com.google.gms.google-services")
}
In all platforms on Application Start you need to initialize library using
//passing android, ios, desktop or web configuration depending on the platform
NotifierManager.initialize(NotificationPlatformConfiguration)
You can send either local or push notification.
Local notifications are supported on Android, iOS, JS and wasm targets. Image is supported on Android and iOS
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")
}
notifer.remove(notificationId) //Removes notification by Id
notifier.removeAll() //Removes all notification
Push notifications are supported only for Android and iOS.
In this method you can send notification token to the server.
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onNewToken(token: String) {
println("onNewToken: $token") //Update user token in the server if needed
}
})
NotifierManager.addListener(object : NotifierManager.Listener {
override onPushNotificationWithPayloadData(title: String?, body: String?, data: PayloadData) {
//PayloadData is just typeAlias for Map<String,*>.
println("Push Notification is received: Title: $title and Body: $body and Notification payloadData: $data")
}
})
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onPushNotification(title:String?,body:String?) {
println("Push Notification notification title: $title")
}
})
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onPayloadData(data: PayloadData) {
println("Push Notification payloadData: $data") //PayloadData is just typeAlias for Map<String,*>.
}
})
And you need to call below platform-specific functions in order to receive payload data properly.
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)
}
Call NotifierManager.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
}
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")
}
})
NotifierManager.getPushNotifier().getToken() //Get current user push notification token
NotifierManager.getPushNotifier().deleteMyToken() //Delete user's token for example when user logs out
NotifierManager.getPushNotifier().subscribeToTopic("new_users")
NotifierManager.getPushNotifier().unSubscribeFromTopic("new_users")
For setting custom notification sound, check https://github.com/mirzemehdi/KMPNotifier/pull/61#issuecomment-2275850021
For setting Intent data in Android (for deeplink), check https://github.com/mirzemehdi/KMPNotifier/pull/60#issue-2454489089
For permissionUtil, or manually asking notification permission check https://github.com/mirzemehdi/KMPNotifier/pull/27#issuecomment-2083639907
If you want to see internal logs of the library, you can set a logger using:
NotifierManager.setLogger { message ->
// Log the message
println(message)
}