How to implement Push Notifications in Kotlin Multiplatform | by Mirzamehdi Karimov | Nov, 2023
In this blog post I will share with you how to implement push notifications using Firebase in Kotlin Multiplatform (targeting iOS and Android platforms).
Push notifications play a crucial role in keeping users engaged and informed about important updates in your Kotlin Multiplatform (KMP) applications. While Firebase provides a robust platform for handling push notifications, integrating it seamlessly across both Android and iOS platforms in a KMP project can be a challenging task since Firebase doesn’t yet support Kotlin Multiplatform. For this purpose we will use the KMPNotifier library. I developed this push notification library to serve as the bridge between your Kotlin Multiplatform codebase and the implementation of push notifications. Whether you’re aiming to send local notifications or push notifications, KMPNotifier provides a clean and efficient API for managing the entire notification lifecycle. With KMPNotifier you can listen for token changes, subscribe or unsubscribe to topics, get current user token, or just send local notifications for any events.
Zeroth Step — Basic setup using Firebase official guideline
Before starting you need to setup basic things using the Firebase official guidelines (like initializing project in Firebase, adding google-services.json
to Android, and GoogleService-Info.plist
to iOS).
Firebase setup for iOS — https://firebase.google.com/docs/ios/setup
Firebase setup for Android— https://firebase.google.com/docs/android/setup
Gradle Setup
KMPNotifier is available on Maven Central. In your root project build.gradle.kts
file (or settings.gradle
file) add mavenCentral()
to repositories, and 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 with latest version in commonMain
. Latest version(https://github.com/mirzemehdi/KMPNotifier/releases):
In iOS framework part you need to 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(project("io.github.mirzemehdi:kmpnotifier:<version>"))
baseName = "shared"
isStatic = true
}
}
And in androidApp build.gradle.kts
file you apply google-services
plugin
plugins {
id("com.android.application")
id("com.google.gms.google-services")
}
Platform Setup
In both platforms on Application Start you need to initialize library using initialize method :
NotifierManager.initialize(NotificationPlatformConfiguration) //passing android or ios configuration depending on the platform
Android Setup
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
NotifierManager.initialize(
configuration = NotificationPlatformConfiguration.Android(
notificationIconResId = R.drawable.ic_launcher_foreground,
)
)
}
}
Also starting from Android 13(API Level 33) you need to ask runtime POST_NOTIFICATIONS
in activity. I created a utility function that you can use in the activity.
val permissionUtil by permissionUtil()
permissionUtil.askNotificationPermission() //this will ask permission in Android 13(API Level 33) or above, otherwise permission will be granted.
iOS Setup
First, you just need to include FirebaseMessaging library to your iOS app from Xcode. Then on application start you need to call both FirebaseApp initialization and NotifierManager initialization methods, and setting apnsToken as below. Don’t forget to add Push Notifications and Background Modes (Remote Notifications) signing capability in Xcode.
import SwiftUI
import shared
import FirebaseCore
import FirebaseMessagingclass AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure() //important
NotifierManager.shared.initialize(configuration: NotificationPlatformConfigurationIos.shared)
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
}
@main
struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Usage
You can send either local or push notification.
Local Notification
val notifier = NotifierManager.getLocalNotifier()
notifier.notify("Title", "Body") //Sends local notification
Push Notification
Listen for push notification token changes -> In this callback 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
}
})
Other useful functions
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")
Related Posts
Leave a Reply Cancel reply
Categories
- ! Без рубрики (1)
- ++PU (1)
- 1 (1)
- 1w (1)
- 1win Brazil (1)
- 1win India (1)
- 1WIN Official In Russia (1)
- 1win Turkiye (1)
- 1xbet egypt (1)
- 2ankarafayansustasi.net_may (1)
- ankarafayansustasi.netsiteai apr (1)
- Artificial intelligence (1)
- Arts & Entertainment, Photography (1)
- belugasitesi_mAY (1)
- BH_TOPsitesi apr (1)
- BHsitesy_may (2)
- Blog (3)
- Bookkeeping (14)
- Bootcamp de programação (2)
- Bootcamp de programación (2)
- BT_TOPsitesi apr (1)
- casino (5)
- casinom-hub (1)
- casinom-hub.comsitesi apr (3)
- colombian mail order brides (1)
- Cryptocurrency exchange (2)
- Dinamobet_next (1)
- Disease & Illness, Colon Cancer (1)
- Dumanbet (1)
- Dumanbet_next (1)
- Finance, Insurance (1)
- FinTech (5)
- Forex Trading (11)
- Galabet (1)
- Health & Fitness, Fitness Equipment (1)
- Hitbet (1)
- Home & Family, Crafts (1)
- Home & Family, Gardening (1)
- Internet Business, Audio-Video Streaming (1)
- Internet Business, Ecommerce (1)
- Internet Business, Email Marketing (1)
- Internet Business, Internet Marketing (1)
- IT Вакансії (1)
- IT Образование (5)
- IT Освіта (1)
- latin women dating (1)
- mail order bride (1)
- Mars bahis (2)
- Matadorbet (1)
- minimiri.comsitesi apr (3)
- Mobile App Development (771)
- Mostbet Russia (1)
- New Post (1)
- News (12)
- PB_TOPsitesi apr (1)
- PBsitesi_may (1)
- Pusulabet (1)
- redmirepool.bizsitesi apr (2)
- redmirepoolsitesi_may (1)
- Reference & Education, College (1)
- Reference & Education, Sociology (1)
- Rokusitesi apr (1)
- Sober living (6)
- Society, Divorce (1)
- Software development (7)
- Superbetin (1)
- Tempobet_next (1)
- thelongeststride.comsitesi apr (1)
- tipobet-turkiyesitesi apr (1)
- Ultrabet (1)
- Uncategorized (1)
- Игра (2)
- казино (1)
- Криптовалюты (1)
- Новости Криптовалют (1)
- Финтех (7)
- Форекс Брокеры (9)
- Форекс обучение (2)