KFirebaseMessaging
KFirebaseMessaging is a Kotlin Multiplatform Mobile (KMM) package that simplifies the integration of Firebase Cloud Messaging (FCM) across Android and iOS platforms. It provides a unified API for handling push notifications and FCM messaging in a shared codebase, allowing developers to seamlessly implement FCM functionality for both platforms without duplicating code

KFirebaseMessaging is available on mavenCentral().
Version Compatibility
| KFirebaseCrashlytics Version | Firebase iOS SDK | Minimum iOS Version |
|---|
| 1.4.0 | Firebase v11.x | iOS 13+ |
| 2.0.0 | Firebase v12.x | iOS 15+ |
- Note v 1.0.2 dependent on
KLocalNotification
- Note add permissions notification needed android and ios
v 2.2.0 add new feature can get notification details
Installation
api("io.github.the-best-is-best:kfirebase-messaging:2.2.0")
api("io.github.the-best-is-best:klocal-notification:1.4.0")
iOS (Using Swift Package Manager - SPM)
Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
- Open your Xcode project.
- Navigate to
File > Add Packages....
- Enter Firebase repository URL:
https://github.com/firebase/firebase-ios-sdk
- Choose
FirebaseCore and add it to your project.
- need add KIOSNotification repository URL:
https://github.com/the-best-is-best/KIOSNotification/tree/0.1.0
First in gradle
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "ComposeApp"
isStatic = true
export("io.github.the-best-is-best:kfirebase-messaging:1.3.1") // Export KLocalNotification so it's available in the framework
export("io.github.the-best-is-best:klocal-notification:1.2.2")
}
}
...
androidMain
AndroidKFirebaseCore.initialize(this)
AndroidKFirebaseMessagingChannel.initialization()
AndroidKMessagingChannel.initialization()
AndroidKFirebaseMessagingChannel().initChannel(
,
,
)
setContent { App() }
dataBundle = intent.extras
(dataBundle != ) {
KFirebaseMessaging.notifyNotificationClicked(dataBundle)
}
{
.onNewIntent(intent)
dataBundle = intent.extras
(dataBundle != ) {
KFirebaseMessaging.notifyNotificationClicked(dataBundle)
}
}
Android Mainifist
iOS (Using Swift Package Manager - SPM)
Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
- Open your Xcode project.
- Navigate to
File > Add Packages....
- Enter Firebase repository URL:
https://github.com/firebase/firebase-ios-sdk
- Choose
FirebaseRemoteConfig and add it to your project.
iosApp AppDelegate example
How use it
LaunchedEffect(Unit) {
KFirebaseMessaging.notificationFlow.collect {
println("notification received is $it")
}
}
LaunchedEffect(Unit) {
LocalNotification.payloadFlow.collect {
println("notification received is $it")
dataNotification = it
}
}
For request permission for android Note ios request added in app delegate
val localNotificationRequest = LocalNotificationRequestAuthorization {
println("permission is $it")
}
ElevatedButton(onClick = {
scope.launch {
val res = localNotificationRequest.launch()
println("per state $res")
}
}) {
Text("Request permissions")
}
For get token fcm
fcm.getToken {
it.onSuccess {
println("token $it")
}
it.onFailure {
println("error token $it")
}
}
For subscribe topic
fcm.subscribeTopic("topic_test", callback = {
it.onSuccess {
println("sub to topic correctly")
}
it.onFailure {
println("sub to topic ${it.message}")
}
})
For un subscribe topic
scope.launch {
fcm.unsubscribeTopic("topic_test", callback = {
it.onSuccess {
println("un sub to topic correctly")
}
it.onFailure {
println("un sub to topic ${it.message}")
}
})
}