{"id":13658286,"url":"https://github.com/mirzemehdi/KMPNotifier","last_synced_at":"2025-04-24T08:31:54.691Z","repository":{"id":206755517,"uuid":"717626777","full_name":"mirzemehdi/KMPNotifier","owner":"mirzemehdi","description":"Kotlin Multiplatform  Push Notification Library targetting android, iOS, Desktop and Web (JS and Wasm)","archived":false,"fork":false,"pushed_at":"2024-08-18T10:35:10.000Z","size":747,"stargazers_count":258,"open_issues_count":4,"forks_count":13,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-08-18T11:44:27.511Z","etag":null,"topics":["android","desktop","desktop-notifications","firebase-push-notification","ios","js","kotlin","kotlin-multiplatform","kotlin-multiplatform-mobile","push-notifications","wasm","web-notifications"],"latest_commit_sha":null,"homepage":"http://mirzemehdi.com/KMPNotifier/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mirzemehdi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-12T03:28:19.000Z","updated_at":"2024-08-18T10:35:13.000Z","dependencies_parsed_at":"2024-05-10T13:51:19.084Z","dependency_job_id":"b2056048-edba-47fe-ab51-48888eea50e1","html_url":"https://github.com/mirzemehdi/KMPNotifier","commit_stats":null,"previous_names":["mirzemehdi/kmpnotifier"],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirzemehdi%2FKMPNotifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirzemehdi%2FKMPNotifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirzemehdi%2FKMPNotifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirzemehdi%2FKMPNotifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mirzemehdi","download_url":"https://codeload.github.com/mirzemehdi/KMPNotifier/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223947303,"owners_count":17230008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","desktop","desktop-notifications","firebase-push-notification","ios","js","kotlin","kotlin-multiplatform","kotlin-multiplatform-mobile","push-notifications","wasm","web-notifications"],"created_at":"2024-08-02T05:00:58.339Z","updated_at":"2025-04-24T08:31:54.656Z","avatar_url":"https://github.com/mirzemehdi.png","language":"Kotlin","funding_links":[],"categories":["Multiplatform","Libraries"],"sub_categories":["Android samples","📱 Device"],"readme":"# KMPNotifier - Kotlin Multiplatform Push Notification\n[![Build](https://github.com/mirzemehdi/KMPNotifier/actions/workflows/build.yml/badge.svg)](https://github.com/mirzemehdi/KMPNotifier/actions/workflows/build.yml) \n[![Kotlin](https://img.shields.io/badge/Kotlin-2.1.10-blue.svg?style=flat\u0026logo=kotlin)](https://kotlinlang.org)\n[![Maven Central](https://img.shields.io/maven-central/v/io.github.mirzemehdi/kmpnotifier?color=blue)](https://search.maven.org/search?q=g:io.github.mirzemehdi)\n\n![badge-android](http://img.shields.io/badge/platform-android-6EDB8D.svg?style=flat)\n![badge-ios](http://img.shields.io/badge/platform-ios-CDCDCD.svg?style=flat)\n![badge-desktop](https://img.shields.io/badge/platform-desktop-3474eb.svg?style=flat)\n![badge-js](https://img.shields.io/badge/platform-js-fcba03.svg?style=flat)\n![badge-wasm](https://img.shields.io/badge/platform-wasm-331f06.svg?style=flat)\n\n[![Android Weekly badge](https://androidweekly.net/issues/issue-632/badge)](https://androidweekly.net/issues/issue-632)\n[![Android Weekly badge](https://androidweekly.net/issues/issue-599/badge)](https://androidweekly.net/issues/issue-599)\n\n\nSimple 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).  \nThis library is used in [FindTravelNow](https://github.com/mirzemehdi/FindTravelNow-KMM/) production KMP project.\nYou can check out [Documentation](https://mirzemehdi.github.io/KMPNotifier) for full library api information.  \n\n![kmpnotifier](https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b)\n\n\n\n**_Related Blog Posts_**  \n[KMPNotifier Update: Web, Desktop, and New Features for Kotlin Multiplatform Notifications](https://proandroiddev.com/kmpnotifier-update-web-desktop-and-new-features-for-kotlin-multiplatform-notifications-529b489f5d9c)  \n[How to implement Push Notifications in Kotlin Multiplatform](https://proandroiddev.com/how-to-implement-push-notification-in-kotlin-multiplatform-5006ff20f76c)  \n\n\n## Features\n  - 🔔 Local Notification (android, ios, desktop, js and wasm targets)  \n  - 🔔 Push Notification (Firebase Cloud Messaging) (android and ios only)  \n  - 📱 Multiplatform (android, iOS, desktop and web (js and wasm))  \n\n## Installation\nBefore 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).\n\n## Minimum Requirements\n\n- **Android:** `minSdkVersion 21`\n- **iOS:** `iOS 14.1`\n\n\n### Gradle Setup\nKMPNotifier 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.\n\n```kotlin\nplugins {\n  id(\"com.android.application\") version \"8.1.3\" apply false\n  id(\"org.jetbrains.kotlin.multiplatform\") version \"1.9.20\" apply false\n  id(\"com.google.gms.google-services\") version \"4.4.0\" apply false\n}\n\nrepositories { \n  mavenCentral()\n}\n```\n\nThen in your shared module you add dependency in `commonMain`. Latest version: [![Maven Central](https://img.shields.io/maven-central/v/io.github.mirzemehdi/kmpnotifier?color=blue)](https://search.maven.org/search?q=g:io.github.mirzemehdi). In iOS framework part export this library as well.\n```kotlin\nsourceSets {\n  commonMain.dependencies {\n    api(\"io.github.mirzemehdi:kmpnotifier:\u003cversion\u003e\") // in iOS export this library\n  }\n}\n\nlistOf(iosX64(),iosArm64(),iosSimulatorArm64()).forEach { iosTarget -\u003e\n  iosTarget.binaries.framework {\n    export(\"io.github.mirzemehdi:kmpnotifier:\u003cversion\u003e\")\n    ...\n  }\n}\n```\n\nAnd in androidApp `build.gradle.kts` file you apply `google-services` plugin  \n```kotlin\nplugins {\n  id(\"com.android.application\")\n  id(\"com.google.gms.google-services\")\n}\n```\n\n\n### Platform Setup\nIn all platforms on Application Start you need to initialize library using \n```kotlin \n//passing android, ios, desktop or web configuration depending on the platform\nNotifierManager.initialize(NotificationPlatformConfiguration)  \n```\n\n\u003cdetails\u003e\n  \u003csummary\u003eAndroid\u003c/summary\u003e\n\n  ### Android Setup\n ```kotlin\nclass MyApplication : Application() {\n    override fun onCreate() {\n        super.onCreate()\n        /**\n         * By default showPushNotification value is true.\n         * When set showPushNotification to false foreground push  notification will not be shown to user.\n         * You can still get notification content using #onPushNotification listener method.\n         */\n        NotifierManager.initialize(\n            configuration = NotificationPlatformConfiguration.Android(\n                notificationIconResId = R.drawable.ic_launcher_foreground,\n                showPushNotification = true,\n            )\n        )\n    }\n}\n```\n\nAlso starting from Android 13(API Level 33) you need to ask runtime `POST_NOTIFICATIONS` in activity. I created utility function that you can use in activity.\n```kotlin\nval permissionUtil by permissionUtil()\npermissionUtil.askNotificationPermission() //this will ask permission in Android 13(API Level 33) or above, otherwise permission will be granted.\n```\n  \n  \n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eiOS\u003c/summary\u003e\n\n  ### iOS Setup\n  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 apnsToken setting as below. Don't forget to add Push Notifications and Background Modes (Remote Notifications) signing capability in Xcode.\n\n```swift\nimport SwiftUI\nimport shared\nimport FirebaseCore\nimport FirebaseMessaging\n\nclass AppDelegate: NSObject, UIApplicationDelegate {\n\n  func application(_ application: UIApplication,\n                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -\u003e Bool {\n\n      FirebaseApp.configure() //important\n      \n      //By default showPushNotification value is true.\n      //When set showPushNotification to false foreground push  notification will not be shown.\n      //You can still get notification content using #onPushNotification listener method.\n      NotifierManager.shared.initialize(configuration: NotificationPlatformConfigurationIos(\n            showPushNotification: true,\n            askNotificationPermissionOnStart: true,\n            notificationSoundName: nil\n          )\n      )\n      \n    return true\n  }\n\n  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {\n        Messaging.messaging().apnsToken = deviceToken\n  }\n    \n}\n\n@main\nstruct iOSApp: App {\n    \n    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate\n    \n    var body: some Scene {\n        WindowGroup {\n            ContentView()\n        }\n    }\n}\n\n\n```\n\n\n\n \n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eDesktop\u003c/summary\u003e\n\n### Desktop Setup\nYou need to put notification icon into resources/common folder. For more information:  \n[Compose Desktop Resources](https://github.com/JetBrains/compose-multiplatform/blob/master/tutorials/Native_distributions_and_local_execution/README.md#packaging-resources)\n ```kotlin\nfun main() = application {\n\n    NotifierManager.initialize(\n        NotificationPlatformConfiguration.Desktop(\n            showPushNotification = true,\n            notificationIconPath = composeDesktopResourcesPath() + File.separator + \"ic_notification.png\"\n        )\n    )\n    \n    AppInitializer.onApplicationStart()\n    Window(\n        onCloseRequest = ::exitApplication,\n        title = \"KMPNotifier Desktop\",\n    ) {\n        println(\"Desktop app is started\")\n        App()\n\n    }\n}\n```  \n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eWeb\u003c/summary\u003e\n\n  \n### Web Setup (Js and Wasm)\nOn application start initialize it using Web configuration\n ```kotlin\nfun main()  {\n\n    NotifierManager.initialize(\n        NotificationPlatformConfiguration.Web(\n            askNotificationPermissionOnStart = true,\n            notificationIconPath = null\n        )\n    )\n    \n}\n```\n**Note:**\nIf you are using mac make sure you also allow notifications for browser from system system settings in order to see web notifications.  \n\n\n\n\n\u003c/details\u003e\n\n## Usage\nYou can send either local or push notification.\n\n### Local Notification\nLocal notifications are supported on Android, iOS, JS and wasm targets. Image is supported on Android and iOS \n#### Send notification\n\n```kotlin\nval notifier = NotifierManager.getLocalNotifier()\nnotifier.notify {\n  id= Random.nextInt(0, Int.MAX_VALUE)\n  title = \"Title from KMPNotifier\"\n  body = \"Body message from KMPNotifier\"\n  payloadData = mapOf(\n    Notifier.KEY_URL to \"https://github.com/mirzemehdi/KMPNotifier/\",\n    \"extraKey\" to \"randomValue\"\n  )\n  image = NotificationImage.Url(\"https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b\")\n}\n```\n\n#### Remove notification by Id or all notifications\n\n```kotlin\nnotifer.remove(notificationId) //Removes notification by Id  \n\nnotifier.removeAll() //Removes all notification\n\n```\n\n### Push Notification\nPush notifications are supported only for Android and iOS.\n#### Listen for push notification token changes\nIn this method you can send notification token to the server.\n\n```kotlin\nNotifierManager.addListener(object : NotifierManager.Listener {\n  override fun onNewToken(token: String) {\n    println(\"onNewToken: $token\") //Update user token in the server if needed\n  }\n}) \n```\n\n#### Receive Notification and Data payload in one callback\n```kotlin\nNotifierManager.addListener(object : NotifierManager.Listener {\n  override onPushNotificationWithPayloadData(title: String?, body: String?, data: PayloadData) {\n    //PayloadData is just typeAlias for Map\u003cString,*\u003e.\n    println(\"Push Notification is received: Title: $title and Body: $body and Notification payloadData: $data\")\n  }\n}) \n```\n\n#### Receive notification type messages  \n```kotlin\nNotifierManager.addListener(object : NotifierManager.Listener {\n  override fun onPushNotification(title:String?,body:String?) {\n    println(\"Push Notification notification title: $title\")\n  }\n}) \n```\n\n\n#### Receive data payload\n```kotlin\nNotifierManager.addListener(object : NotifierManager.Listener {\n  override fun onPayloadData(data: PayloadData) {\n    println(\"Push Notification payloadData: $data\") //PayloadData is just typeAlias for Map\u003cString,*\u003e.\n  }\n}) \n```\nAnd you need to call below platform-specific functions in order to receive payload data properly.\n##### Android\nCall `NotifierManager.onCreateOrOnNewIntent(intent)` on launcher Activity's `onCreate` and `onNewIntent` methods.\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n   super.onCreate(savedInstanceState)\n      NotifierManager.onCreateOrOnNewIntent(intent)\n      ...\n    }\n\n    override fun onNewIntent(intent: Intent?) {\n        super.onNewIntent(intent)\n        NotifierManager.onCreateOrOnNewIntent(intent)\n    }\n\n```\n\n##### iOS\nCall `NotifierManager.onApplicationDidReceiveRemoteNotification(userInfo: userInfo)` on application's `didReceiveRemoteNotification` method.\n\n```\n func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -\u003e UIBackgroundFetchResult {\n      NotifierManager.shared.onApplicationDidReceiveRemoteNotification(userInfo: userInfo)\n      return UIBackgroundFetchResult.newData\n }\n\n```  \n\n\n#### Detecting notification click and get payload data\nMake sure you follow previous step for getting payload data properly.\n```kotlin\nNotifierManager.addListener(object : NotifierManager.Listener {\n    override fun onNotificationClicked(data: PayloadData) {\n        super.onNotificationClicked(data)\n        println(\"Notification clicked, Notification payloadData: $data\")\n    }\n}) \n```   \n\n\n#### Other functions\n```kotlin\nNotifierManager.getPushNotifier().getToken() //Get current user push notification token\nNotifierManager.getPushNotifier().deleteMyToken() //Delete user's token for example when user logs out \nNotifierManager.getPushNotifier().subscribeToTopic(\"new_users\") \nNotifierManager.getPushNotifier().unSubscribeFromTopic(\"new_users\") \n```\nFor setting custom notification sound, check https://github.com/mirzemehdi/KMPNotifier/pull/61#issuecomment-2275850021  \nFor setting Intent data in Android (for deeplink), check https://github.com/mirzemehdi/KMPNotifier/pull/60#issue-2454489089    \nFor permissionUtil, or manually asking notification permission check https://github.com/mirzemehdi/KMPNotifier/pull/27#issuecomment-2083639907  \n\n### Logging\n\nIf you want to see internal logs of the library, you can set a logger using:\n\n```kotlin\nNotifierManager.setLogger { message -\u003e\n    // Log the message\n    println(message)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmirzemehdi%2FKMPNotifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmirzemehdi%2FKMPNotifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmirzemehdi%2FKMPNotifier/lists"}