
Simplifies integration of Firebase Cloud Messaging, offering a unified API for handling push notifications and messaging, enabling seamless implementation across platforms without code duplication.
KFirebaseMessaging is available on mavenCentral().
| KFirebaseCrashlytics Version | Firebase iOS SDK | Minimum iOS Version |
|---|---|---|
| 1.4.0 | Firebase v11.x | iOS 13+ |
| 2.0.0 | Firebase v12.x | iOS 15+ |
api("io.github.the-best-is-best:kfirebase-messaging:2.2.0")
api("io.github.the-best-is-best:klocal-notification:1.4.0")
Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
File > Add Packages....https://github.com/firebase/firebase-ios-sdk
FirebaseCore and add it to your project.https://github.com/the-best-is-best/KIOSNotification/tree/0.1.0
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")
}
}
...
AndroidKFirebaseCore.initialize(this)
AndroidKFirebaseMessagingChannel.initialization(this)
AndroidKMessagingChannel.initialization(this)
AndroidKFirebaseMessagingChannel().initChannel(
"fcm",
"fcm notification",
"ic_notification"
)
//this for get data fcm when app reopen
// already added
setContent { App() }
val dataBundle = intent.extras
if (dataBundle != null) {
KFirebaseMessaging.notifyNotificationClicked(dataBundle)
}
// for get data fcm in app background
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
val dataBundle = intent.extras
if (dataBundle != null) {
KFirebaseMessaging.notifyNotificationClicked(dataBundle)
}
}
<meta-data android:name="com.google.firebase.messaging.background_enabled"
android:value="true" /><meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" /><meta-data
android:name="com.google.firebase.messaging.default_notification_channel_name"
android:value="@string/default_notification_channel_name" /><!-- optional -->
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/custom_color" />
Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
File > Add Packages....https://github.com/firebase/firebase-ios-sdk
FirebaseRemoteConfig and add it to your project.import ComposeApp
import Firebase
import UIKit
import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate,
MessagingDelegate
{
var window: UIWindow?
// This function is called when the app starts
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Firebase initialization
FirebaseApp.configure()
LocalNotification.shared.doInit(userNotificationCenterDelegate: self)
AppleKFirebaseMessaging().doInit(messagingDelegate: self)
window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
window.rootViewController = MainViewControllerKt.MainViewController()
window.makeKeyAndVisible()
}
// not need add this now
if let userInfo = launchOptions?[.remoteNotification] as? [String: AnyObject] {
LocalNotification.shared.notifyPayloadListeners(data: userInfo)
}
return true
}
// Handle failure to register for remote notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error.localizedDescription)")
}
// Handle notification when the app is in the foreground
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNS Token: \(deviceToken)")
Messaging.messaging().apnsToken = deviceToken
}
// Handle notification when the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
let title = notification.request.content.title
let body = notification.request.content.body
let fromTopic = userInfo["from"] as? String
let payload = userInfo.reduce(into: [String: Any]()) { result, entry in
if let key = entry.key as? String {
result[key] = entry.value
}
}
let data = FirebaseNotificationData(
title: title,
body: body,
payload: payload,
fromTopic: fromTopic
)
KFirebaseMessaging.shared.notifyNotificationListener(data: data)
completionHandler([.alert, .sound, .badge]) // Show notification in the foreground
}
// Handle notification when the user interacts with it (taps on the notification)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
LocalNotification.shared.notifyPayloadListeners(data: userInfo)
completionHandler()
}
// Firebase Messaging delegate method for receiving FCM token
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
KFirebaseMessaging().notifyTokenListener(token: fcmToken)
}
}
// v2.2.0 add
LaunchedEffect(Unit) {
KFirebaseMessaging.notificationFlow.collect {
println("notification received is $it")
}
}
LaunchedEffect(Unit) {
LocalNotification.payloadFlow.collect {
println("notification received is $it")
dataNotification = it
}
}
val localNotificationRequest = LocalNotificationRequestAuthorization {
println("permission is $it")
}
ElevatedButton(onClick = {
scope.launch {
val res = localNotificationRequest.launch()
println("per state $res")
}
}) {
Text("Request permissions")
}
fcm.getToken {
it.onSuccess {
println("token $it")
}
it.onFailure {
println("error token $it")
}
}fcm.subscribeTopic("topic_test", callback = {
it.onSuccess {
println("sub to topic correctly")
}
it.onFailure {
println("sub to topic ${it.message}")
}
})scope.launch {
fcm.unsubscribeTopic("topic_test", callback = {
it.onSuccess {
println("un sub to topic correctly")
}
it.onFailure {
println("un sub to topic ${it.message}")
}
})
}KFirebaseMessaging is available on mavenCentral().
| KFirebaseCrashlytics Version | Firebase iOS SDK | Minimum iOS Version |
|---|---|---|
| 1.4.0 | Firebase v11.x | iOS 13+ |
| 2.0.0 | Firebase v12.x | iOS 15+ |
api("io.github.the-best-is-best:kfirebase-messaging:2.2.0")
api("io.github.the-best-is-best:klocal-notification:1.4.0")
Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
File > Add Packages....https://github.com/firebase/firebase-ios-sdk
FirebaseCore and add it to your project.https://github.com/the-best-is-best/KIOSNotification/tree/0.1.0
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")
}
}
...
AndroidKFirebaseCore.initialize(this)
AndroidKFirebaseMessagingChannel.initialization(this)
AndroidKMessagingChannel.initialization(this)
AndroidKFirebaseMessagingChannel().initChannel(
"fcm",
"fcm notification",
"ic_notification"
)
//this for get data fcm when app reopen
// already added
setContent { App() }
val dataBundle = intent.extras
if (dataBundle != null) {
KFirebaseMessaging.notifyNotificationClicked(dataBundle)
}
// for get data fcm in app background
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
val dataBundle = intent.extras
if (dataBundle != null) {
KFirebaseMessaging.notifyNotificationClicked(dataBundle)
}
}
<meta-data android:name="com.google.firebase.messaging.background_enabled"
android:value="true" /><meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" /><meta-data
android:name="com.google.firebase.messaging.default_notification_channel_name"
android:value="@string/default_notification_channel_name" /><!-- optional -->
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/custom_color" />
Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
File > Add Packages....https://github.com/firebase/firebase-ios-sdk
FirebaseRemoteConfig and add it to your project.import ComposeApp
import Firebase
import UIKit
import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate,
MessagingDelegate
{
var window: UIWindow?
// This function is called when the app starts
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Firebase initialization
FirebaseApp.configure()
LocalNotification.shared.doInit(userNotificationCenterDelegate: self)
AppleKFirebaseMessaging().doInit(messagingDelegate: self)
window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
window.rootViewController = MainViewControllerKt.MainViewController()
window.makeKeyAndVisible()
}
// not need add this now
if let userInfo = launchOptions?[.remoteNotification] as? [String: AnyObject] {
LocalNotification.shared.notifyPayloadListeners(data: userInfo)
}
return true
}
// Handle failure to register for remote notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error.localizedDescription)")
}
// Handle notification when the app is in the foreground
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNS Token: \(deviceToken)")
Messaging.messaging().apnsToken = deviceToken
}
// Handle notification when the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
let title = notification.request.content.title
let body = notification.request.content.body
let fromTopic = userInfo["from"] as? String
let payload = userInfo.reduce(into: [String: Any]()) { result, entry in
if let key = entry.key as? String {
result[key] = entry.value
}
}
let data = FirebaseNotificationData(
title: title,
body: body,
payload: payload,
fromTopic: fromTopic
)
KFirebaseMessaging.shared.notifyNotificationListener(data: data)
completionHandler([.alert, .sound, .badge]) // Show notification in the foreground
}
// Handle notification when the user interacts with it (taps on the notification)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
LocalNotification.shared.notifyPayloadListeners(data: userInfo)
completionHandler()
}
// Firebase Messaging delegate method for receiving FCM token
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
KFirebaseMessaging().notifyTokenListener(token: fcmToken)
}
}
// v2.2.0 add
LaunchedEffect(Unit) {
KFirebaseMessaging.notificationFlow.collect {
println("notification received is $it")
}
}
LaunchedEffect(Unit) {
LocalNotification.payloadFlow.collect {
println("notification received is $it")
dataNotification = it
}
}
val localNotificationRequest = LocalNotificationRequestAuthorization {
println("permission is $it")
}
ElevatedButton(onClick = {
scope.launch {
val res = localNotificationRequest.launch()
println("per state $res")
}
}) {
Text("Request permissions")
}
fcm.getToken {
it.onSuccess {
println("token $it")
}
it.onFailure {
println("error token $it")
}
}fcm.subscribeTopic("topic_test", callback = {
it.onSuccess {
println("sub to topic correctly")
}
it.onFailure {
println("sub to topic ${it.message}")
}
})scope.launch {
fcm.unsubscribeTopic("topic_test", callback = {
it.onSuccess {
println("un sub to topic correctly")
}
it.onFailure {
println("un sub to topic ${it.message}")
}
})
}