Skip to content

Commit

Permalink
Merge pull request #26 from mirzemehdi/payload_data
Browse files Browse the repository at this point in the history
Fixing android payload data is always empty onNotificationClicked
  • Loading branch information
mirzemehdi authored Apr 29, 2024
2 parents 4ecc017 + b9bb110 commit 4d2a2e4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
9 changes: 7 additions & 2 deletions kmpnotifier/api/kmpnotifier.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>): 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<String, String>) {
permissionUtil.hasNotificationPermission {
if (it.not())
Log.w(
Expand All @@ -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,
Expand Down Expand Up @@ -66,21 +66,15 @@ internal class AndroidNotifier(
notificationManager.cancelAll()
}

private fun getPendingIntent(deepLink: String = ""): PendingIntent? {
private fun getPendingIntent(payloadData: Map<String, String>): 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> = 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<String, String> = emptyMap()
)


/**
* Remove notification by id
* @param id notification id
*/
public fun remove(id:Int)
public fun remove(id: Int)

/**
* Removes all previously shown notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>): 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<String, String>) {
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(
Expand Down Expand Up @@ -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
)
}
}
}
Expand Down

0 comments on commit 4d2a2e4

Please sign in to comment.