In my previous blog post, I introduced KMPNotifier, a Kotlin Multiplatform library designed to simplify local and push notification implementation across Android and iOS platforms using Firebase. I’m happy to share that it has now reached almost a quarter-thousand stars on GitHub! :). Since then, KMPNotifier has improved with new features like support for Web (JavaScript (JS) and WebAssembly (WASM)) and Desktop platforms, the option to add custom notification sounds on Android and iOS, deep linking support on Android, better handling of notification permissions on initialization of the library, and new listener methods for customizing notification behaviors. It also supports Kotlin 2.0 now. In this post, I’ll walk you through these updates and show you how to make the most of the latest version of KMPNotifier.
New Platforms: Web and Desktop Support
One of the most exciting updates to KMPNotifier is the addition of web support, including JavaScript (JS) and WebAssembly (WASM) targets. This means you can now send and receive local notifications in web applications built with Kotlin Multiplatform.
Here’s how you can initialize KMPNotifier for the web:
NotifierManager.initialize(
NotificationPlatformConfiguration.Web(
askNotificationPermissionOnStart = true,
notificationIconPath = null //Any image icon url or put image in resources folder
)
)
NOTE: For macOS users, be sure to enable notifications for your browser in the system settings to ensure that web notifications display correctly.
In addition to web support, KMPNotifier now also supports local notifications on Desktop platforms. Desktop initialization:
NotifierManager.initialize(
NotificationPlatformConfiguration.Desktop(
notificationIconPath = composeDesktopResourcesPath() + File.separator + "ic_notification.png"
)
)
For custom notification icon on Desktop, you need to put notification icon into resources/common folder. For more information: Compose Desktop Resources.
Custom Notification Sounds
KMPNotifier now has the ability to use custom notification sounds on both Android and iOS.
Android: To set a custom notification sound, place your audio file in the res/raw directory and initialize the library with the soundUri parameter:
val customNotificationSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + "YOUR_APPLICATION_PACKAGE_NAME" + "/" + R.raw.custom_notification_sound)
NotifierManager.initialize(
configuration = NotificationPlatformConfiguration.Android(
notificationIconResId = R.drawable.ic_launcher_foreground,
showPushNotification = true,
notificationChannelData = NotificationPlatformConfiguration.Android.NotificationChannelData(
soundUri = customNotificationSound.toString()
)
)
)
iOS: For iOS, add your custom sound file to the Resources directory, then specify the notificationSoundName parameter during initialization:
NotifierManager.initialize(
NotificationPlatformConfiguration.Ios(
showPushNotification = true,
askNotificationPermissionOnStart = true,
notificationSoundName = "custom_notification_sound.wav"
)
)
If no custom sound is specified, the default notification sound will be used.
Deep Link Support on Android
KMPNotifier now supports deep linking on Android, allowing you to associate notifications with specific actions within your app via URIs. To utilize this feature, include a “URL” key in your notification payload data:
notifier.notify(
title = "Title",
body = "bodyMessage",
payloadData = mapOf(
Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/",
"extraKey" to "randomValue"
)
)
KMPNotifier will automatically set the Intent’s data to the URI provided in the “URL” key, enabling deep linking within your Android app.
Notification Permission Handling
In previous versions of KMPNotifier, notification permissions were requested automatically when the app started. The latest update introduces more flexibility, allowing developers to control when these permissions are requested. To manage notification permissions manually, set askNotificationPermissionOnStart to false during initialization. You can then use the PermissionUtil class or Moko Permissions to request permissions when needed in your app.
val permissionUtil = NotifierManager.getPermissionUtil()
permissionUtil.askNotificationPermission()
New Listener Methods for Advanced Notification Handling
The latest update to KMPNotifier introduces new listener methods that improve to manage notifications effectively. These methods allow for greater customization and control over how notifications are handled within your application.
NotifierManager.addListener(object : NotifierManager.Listener {
override fun onNewToken(token: String) {
println("Push Notification onNewToken: $token")
}
override fun onPushNotification(title: String?, body: String?) {
super.onPushNotification(title, body)
println("Push Notification notification type message is received: Title: $title and Body: $body")
}
override fun onPayloadData(data: PayloadData) {
super.onPayloadData(data)
println("Push Notification data type message is received, payloadData: $data")
}
override fun onNotificationClicked(data: PayloadData) {
super.onNotificationClicked(data)
println("Notification clicked, Notification payloadData: $data")
}
})
Note: By default, foreground push notifications are shown to the user. If you want to customize this behavior, you can set the showPushNotification value to false when initializing the library. This way, notifications will not be displayed to the user, but you can still access the notification content using the onPushNotification listener method mentioned above.
Notification Removal Functionality
KMPNotifier now has a support to manage notifications by removing them when needed:
notifier.remove(notificationId) //Remove notification by id.
notifier.removeAll() //Remove all notifications
Thank you all for your support and collaboration as we improve KMPNotifier! With these updates, KMPNotifier is now available for managing notifications across Android, iOS, web, and desktop platforms. Don’t forget to check out the latest updates on our GitHub page.
KMPNotifier Update: Web, Desktop, and New Features for Kotlin Multiplatform Notifications was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.
[Read on Medium]