diff --git a/kmpnotifier/api/kmpnotifier.api b/kmpnotifier/api/kmpnotifier.api index 055ff0b..e187a53 100644 --- a/kmpnotifier/api/kmpnotifier.api +++ b/kmpnotifier/api/kmpnotifier.api @@ -3,12 +3,17 @@ public final class com/mmk/kmpnotifier/extensions/NotifierManagerExtKt { } public abstract interface class com/mmk/kmpnotifier/notification/Notifier { - public abstract fun notify (ILjava/lang/String;Ljava/lang/String;)V - public abstract fun notify (Ljava/lang/String;Ljava/lang/String;)I + public abstract fun notify (ILjava/lang/String;Ljava/lang/String;Ljava/util/Map;)V + public abstract fun notify (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)I public abstract fun remove (I)V public abstract fun removeAll ()V } +public final class com/mmk/kmpnotifier/notification/Notifier$DefaultImpls { + public static synthetic fun notify$default (Lcom/mmk/kmpnotifier/notification/Notifier;ILjava/lang/String;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V + public static synthetic fun notify$default (Lcom/mmk/kmpnotifier/notification/Notifier;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)I +} + public final class com/mmk/kmpnotifier/notification/NotifierManager { public static final field INSTANCE Lcom/mmk/kmpnotifier/notification/NotifierManager; public final fun addListener (Lcom/mmk/kmpnotifier/notification/NotifierManager$Listener;)V diff --git a/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/firebase/MyFirebaseMessagingService.kt b/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/firebase/MyFirebaseMessagingService.kt index 0bffd60..5d26be6 100644 --- a/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/firebase/MyFirebaseMessagingService.kt +++ b/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/firebase/MyFirebaseMessagingService.kt @@ -21,14 +21,20 @@ internal class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(message: RemoteMessage) { super.onMessageReceived(message) + val payloadData = message.data message.notification?.let { if (notifierManager.shouldShowNotification()) - notifier.notify(it.title ?: "", it.body ?: "") + notifier.notify( + title = it.title ?: "", + body = it.body ?: "", + payloadData = payloadData + ) notifierManager.onPushNotification(title = it.title, body = it.body) } - if (message.data.isNotEmpty()) { - val data = message.data + mapOf( Constants.ACTION_NOTIFICATION_CLICK to Constants.ACTION_NOTIFICATION_CLICK) + if (payloadData.isNotEmpty()) { + val data = + payloadData + mapOf(Constants.ACTION_NOTIFICATION_CLICK to Constants.ACTION_NOTIFICATION_CLICK) notifierManager.onPushPayloadData(data) } } diff --git a/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/notification/AndroidNotifier.kt b/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/notification/AndroidNotifier.kt index 6c5bbc2..a51cb67 100644 --- a/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/notification/AndroidNotifier.kt +++ b/kmpnotifier/src/androidMain/kotlin/com/mmk/kmpnotifier/notification/AndroidNotifier.kt @@ -21,13 +21,13 @@ internal class AndroidNotifier( private val permissionUtil: PermissionUtil, ) : Notifier { - override fun notify(title: String, body: String): Int { + override fun notify(title: String, body: String, payloadData: Map): Int { val notificationID = Random.nextInt(0, Int.MAX_VALUE) - notify(notificationID, title, body) + notify(notificationID, title, body, payloadData) return notificationID } - override fun notify(id: Int, title: String, body: String) { + override fun notify(id: Int, title: String, body: String, payloadData: Map) { permissionUtil.hasNotificationPermission { if (it.not()) Log.w( @@ -36,7 +36,7 @@ internal class AndroidNotifier( ) } val notificationManager = context.notificationManager ?: return - val pendingIntent = getPendingIntent() + val pendingIntent = getPendingIntent(payloadData) notificationChannelFactory.createChannels() val notification = NotificationCompat.Builder( context, @@ -66,21 +66,15 @@ internal class AndroidNotifier( notificationManager.cancelAll() } - private fun getPendingIntent(deepLink: String = ""): PendingIntent? { + private fun getPendingIntent(payloadData: Map): PendingIntent? { val intent = getLauncherActivityIntent()?.apply { putExtra(ACTION_NOTIFICATION_CLICK, ACTION_NOTIFICATION_CLICK) + payloadData.forEach { putExtra(it.key, it.value) } } intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - val flags = - PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - if (deepLink.isBlank().not() && intent != null) { - with(intent) { - action = Intent.ACTION_VIEW - data = Uri.parse(deepLink) - addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - } - } + val flags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + return PendingIntent.getActivity(context, 0, intent, flags) } diff --git a/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/Notifier.kt b/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/Notifier.kt index 663fd21..018a3a0 100644 --- a/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/Notifier.kt +++ b/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/Notifier.kt @@ -9,25 +9,35 @@ public interface Notifier { * Sends local notification to device * @param title Title part * @param body Body part + * @param payloadData Extra payload data information. * @return notification id */ - public fun notify(title: String, body: String): Int + public fun notify( + title: String, + body: String, + payloadData: Map = emptyMap() + ): Int /** * Sends local notification to device with id * @param id notification id * @param title Title part * @param body Body part + * @param payloadData Extra payload data information */ - public fun notify(id:Int, title: String, body: String) - + public fun notify( + id: Int, + title: String, + body: String, + payloadData: Map = emptyMap() + ) /** * Remove notification by id * @param id notification id */ - public fun remove(id:Int) + public fun remove(id: Int) /** * Removes all previously shown notifications diff --git a/kmpnotifier/src/iosMain/kotlin/com/mmk/kmpnotifier/notification/IosNotifier.kt b/kmpnotifier/src/iosMain/kotlin/com/mmk/kmpnotifier/notification/IosNotifier.kt index c938ff8..948fb9f 100644 --- a/kmpnotifier/src/iosMain/kotlin/com/mmk/kmpnotifier/notification/IosNotifier.kt +++ b/kmpnotifier/src/iosMain/kotlin/com/mmk/kmpnotifier/notification/IosNotifier.kt @@ -24,18 +24,19 @@ internal class IosNotifier( ) : Notifier { - override fun notify(title: String, body: String): Int { + override fun notify(title: String, body: String, payloadData: Map): Int { val notificationID = Random.nextInt(0, Int.MAX_VALUE) - notify(notificationID, title, body) + notify(notificationID, title, body, payloadData) return notificationID } - override fun notify(id: Int, title: String, body: String) { + override fun notify(id: Int, title: String, body: String, payloadData: Map) { permissionUtil.askNotificationPermission { val notificationContent = UNMutableNotificationContent().apply { setTitle(title) setBody(body) setSound(UNNotificationSound.defaultSound) + setUserInfo(userInfo + payloadData) } val trigger = UNTimeIntervalNotificationTrigger.triggerWithTimeInterval(1.0, false) val notificationRequest = UNNotificationRequest.requestWithIdentifier( @@ -77,7 +78,9 @@ internal class IosNotifier( ) { val notificationContent = willPresentNotification.request.content NotifierManager.onUserNotification(notificationContent) - if (NotifierManager.shouldShowNotification(notificationContent)) withCompletionHandler(IosPermissionUtil.NOTIFICATION_PERMISSIONS) + if (NotifierManager.shouldShowNotification(notificationContent)) withCompletionHandler( + IosPermissionUtil.NOTIFICATION_PERMISSIONS + ) } } }