Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/line/abc-kmm-notifications
Remote Notification Manager for Kotlin Multiplatform Mobile iOS and Android
https://github.com/line/abc-kmm-notifications
Last synced: about 1 month ago
JSON representation
Remote Notification Manager for Kotlin Multiplatform Mobile iOS and Android
- Host: GitHub
- URL: https://github.com/line/abc-kmm-notifications
- Owner: line
- License: apache-2.0
- Created: 2021-10-15T03:11:28.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-20T22:40:11.000Z (over 1 year ago)
- Last Synced: 2024-08-02T05:09:04.781Z (4 months ago)
- Language: Kotlin
- Homepage:
- Size: 207 KB
- Stars: 56
- Watchers: 9
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- kmp-awesome - abc-kmm-notifications - Notification Manager (Libraries / 📱 Device)
README
![abc-kmm-notifications: Remote Notification Manager](images/cover.png)
[![Kotlin](https://img.shields.io/badge/kotlin-1.5.21-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![KMM](https://img.shields.io/badge/KMM-0.2.7-lightgreen.svg?logo=KMM)](https://plugins.jetbrains.com/plugin/14936-kotlin-multiplatform-mobile)
[![AGP](https://img.shields.io/badge/AGP-7.0.1-green.svg?logo=AGP)](https://developer.android.com/studio/releases/gradle-plugin)
[![Gradle](https://img.shields.io/badge/Gradle-7.0.2-blue.svg?logo=Gradle)](https://gradle.org)
[![Platform](https://img.shields.io/badge/platform-ios,android-lightgray.svg?style=flat)](https://img.shields.io/badge/platform-ios-lightgray.svg?style=flat)Remote Notification Manager for [Kotlin Multiplatform Mobile](https://kotlinlang.org/docs/mpp-intro.html)
## Features
- Super easy to use APNs and FCM in one interface
- Dramatically reduce code to write
- Support for generic model mapping
- Support FCM for iOS
- Migration Support for React Native## Requirements
- iOS
- Deployment Target 10.0 or higher
- Android
- minSdkVersion 21## Installation
### Gradle Settings
Add below gradle settings into your KMP (Kotlin Multiplatform Project)
#### build.gradle.kts in root
```kotlin
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21")
classpath("org.jetbrains.kotlin:kotlin-serialization:1.5.21")
classpath("com.google.gms:google-services:4.3.5")
}
}
```
#### build.gradle.kts in shared
```kotlin
plugins {
id("com.android.library")
kotlin("multiplatform")
kotlin("plugin.serialization")
}val abcNotifications = "com.linecorp.abc:kmm-notifications:0.4.1"
val kotlinxSerialization = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"kotlin {
sourceSets {
ios {
binaries
.filterIsInstance()
.forEach {
it.transitiveExport = true
it.export(abcNotifications)
}
}
android()val commonMain by getting {
dependencies {
implementation(abcNotifications)
implementation(kotlinxSerialization)
}
}
val androidMain by getting {
dependencies {
implementation(abcNotifications)
api(abcNotifications)
}
}
val iosMain by getting {
dependencies {
implementation(abcNotifications)
api(abcNotifications)
}
}
}
}
```#### build.gradle.kts in androidApp
```kotlin
plugins {
id("com.google.gms.google-services")
}
```## Usage
### Definition of payload and map function with generic in commonMain of your project
```kotlin
@Serializable
data class Data(
val notificationType: String = "",
val id: Int = 0,
)@Throws(Throwable::class)
fun NotificationElement.payload() = decodedPayload()
```### Android
```kotlin
ABCNotifications
.onNewToken(this) {
// TODO: send to register ${ABCDeviceToken.value} to server
}
.onDeletedMessages(this) {
// TODO: sync messages to server
}
.onMessageReceived(this) {
// FCM RemoteMessage
val remoteMessage = it.remoteMessage
// decode to Payload with Data
val payload = it.payload()// decode to Data
val data = it.decodedData()// TODO: present a dialog for push notification
}
.beginListening()
```### iOS
```swift
ABCNotifications.Companion()
.registerSettings {
$0.add(type: .alert)
$0.add(type: .badge)
$0.add(type: .sound)
}
.onNewToken(target: self) {
// TODO: send to register ${ABCDeviceToken.rawValue} to server
}
.onMessageReceived(target: self) {
guard let payload = try? $0.payload() else { return }if $0.isInactive {
// TODO: present a view controller on inactive
} else {
// TODO: present a toast message on active
}
}.beginListening()
```#### Required implementation in AppDelegate
```swift
class AppDelegate: UIResponder, UIApplicationDelegate {func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ABCNotificationCenterDelegate.Companion().applicationDidFinishLaunching(options: options)
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
ABCNotificationCenterDelegate.Companion().applicationDidRegisterForRemoteNotifications(deviceToken: deviceToken)
}
}extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
ABCNotificationCenterDelegate.Companion().userNotificationCenterWillPresent(notification: notification)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
ABCNotificationCenterDelegate.Companion().userNotificationCenterDidReceive(response: response)
}
}
```#### FCM on iOS
1. Insert the dependency in shared.podspec
```ruby
spec.dependency 'FirebaseMessaging'
```
2. Insert function into cocoapods definition in build.gradle.kts (shared)
```kotlin
cocoapods {
noPodspec()
}
```3. Initializing
```swift
ABCNotifications.Companion()
.allowsFCMOnIOS()
.registerSettings {
$0.add(type: .alert)
$0.add(type: .badge)
$0.add(type: .sound)
}
.onNewToken(target: self) {
print("onNewToken -> ", ABCDeviceToken.Companion().FCMToken)
// TODO: send to register ${ABCDeviceToken.FCMToken} to server
}.beginListening()
```## Advanced
### Shared configuration in commonMain
```kotlinfun ABCNotifications.Companion.configure(block: ABCNotifications.Companion.() -> Unit) {
apply(block)onNewToken(this) {
// TODO: send to register ${ABCDeviceToken.value} to server
}.beginListening()
}
```### Android
```kotlinABCNotifications.configure {
onDeletedMessages(this) {
// TODO: sync messages to server
}
onMessageReceived(this) {
// FCM RemoteMessage
val remoteMessage = it.remoteMessage// decode to Payload with Data
val payload = it.payload()// decode to Data
val data = it.decodedData()// TODO: present a dialog for push notification
}
}
```### iOS
```swift
ABCNotifications.Companion().configure { [unowned self] in
$0.registerSettings {
$0.add(type: .alert)
$0.add(type: .badge)
$0.add(type: .sound)
}
$0.onMessageReceived(target: self) {
guard let payload = try? $0.payload() else { return }if $0.isInactive {
// TODO: present a view controller on inactive
} else {
// TODO: present a toast message on active
}
}
}
```