Skip to content

Commit

Permalink
Merge pull request #9 from GetStream/push-messages-extra
Browse files Browse the repository at this point in the history
[i116] Extend PushMessage to access extra data
  • Loading branch information
kanat authored Sep 5, 2023
2 parents 8835733 + d14a66c commit c884031
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public final class io/getstream/android/push/delegate/BuildConfig {
public abstract class io/getstream/android/push/delegate/PushDelegate {
public fun <init> (Landroid/content/Context;)V
public final fun getContext ()Landroid/content/Context;
public abstract fun handlePushMessage (Ljava/util/Map;)Z
public abstract fun handlePushMessage (Ljava/util/Map;Ljava/util/Map;)Z
public abstract fun registerPushDevice (Lio/getstream/android/push/PushDevice;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public abstract class PushDelegate(public val context: Context) {
* If the payload can't be handled because doesn't contain the needed data, return false to notify you this
* push message payload needs to be handled by you.
*
* @param metadata The metadata of a remote message.
* @param payload The payload to be handled.
* @return True if the payload was handled.
*/
public abstract fun handlePushMessage(payload: Map<String, Any?>): Boolean
public abstract fun handlePushMessage(metadata: Map<String, Any?>, payload: Map<String, Any?>): Boolean

/**
* Register a new [PushDevice]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public object FirebaseMessagingDelegate {
*/
@JvmStatic
public fun handleRemoteMessage(remoteMessage: RemoteMessage): Boolean {
return PushDelegateProvider.delegates.any { it.handlePushMessage(remoteMessage.data) }
return PushDelegateProvider.delegates.any {
it.handlePushMessage(
metadata = remoteMessage.extractMetadata(),
payload = remoteMessage.data,
)
}
}

/**
Expand All @@ -64,4 +69,19 @@ public object FirebaseMessagingDelegate {
PushDelegateProvider.delegates.forEach { delegate -> delegate.registerPushDevice(it) }
}
}

private fun RemoteMessage.extractMetadata(): Map<String, Any> {
return hashMapOf<String, Any>().apply {
senderId?.also { put("firebase.sender_id", it) }
from?.also { put("firebase.from", it) }
to?.also { put("firebase.to", it) }
messageType?.also { put("firebase.message_type", it) }
messageId?.also { put("firebase.message_id", it) }
collapseKey?.also { put("firebase.collapse_key", it) }
put("firebase.sent_time", sentTime)
put("firebase.ttl", ttl)
put("firebase.priority", priority)
put("firebase.priority", originalPriority)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public object HuaweiMessagingDelegate {
@Throws(IllegalStateException::class)
@JvmStatic
public fun handleRemoteMessage(remoteMessage: RemoteMessage): Boolean {
return PushDelegateProvider.delegates.any { it.handlePushMessage(remoteMessage.dataOfMap) }
return PushDelegateProvider.delegates.any {
it.handlePushMessage(
metadata = remoteMessage.extractMetadata(),
payload = remoteMessage.dataOfMap,
)
}
}

/**
Expand All @@ -67,4 +72,23 @@ public object HuaweiMessagingDelegate {
PushDelegateProvider.delegates.forEach { delegate -> delegate.registerPushDevice(it) }
}
}

private fun RemoteMessage.extractMetadata(): Map<String, Any> {
return hashMapOf<String, Any>().apply {
from?.also { put("huawei.from", it) }
to?.also { put("huawei.to", it) }
messageType?.also { put("huawei.message_type", it) }
messageId?.also { put("huawei.message_id", it) }
collapseKey?.also { put("huawei.collapse_key", it) }
analyticInfoMap?.also { put("huawei.analytic_info_map", it) }
token?.also { put("huawei.token", token) }
put("huawei.sent_time", sentTime)
put("huawei.send_mode", sendMode)
put("huawei.receipt_mode", receiptMode)
put("huawei.urgency", urgency)
put("huawei.original_urgency", originalUrgency)
put("huawei.original_urgency", originalUrgency)
put("huawei.ttl", ttl)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public object XiaomiMessagingDelegate {
*/
@JvmStatic
public fun handleMiPushMessage(miPushMessage: MiPushMessage): Boolean {
return PushDelegateProvider.delegates.any { it.handlePushMessage(miPushMessage.contentMap) }
return PushDelegateProvider.delegates.any {
it.handlePushMessage(
metadata = miPushMessage.extractMetadata(),
payload = miPushMessage.contentMap,
)
}
}

/**
Expand Down Expand Up @@ -89,4 +94,23 @@ public object XiaomiMessagingDelegate {
*/
private val MiPushMessage.contentMap: Map<String, String>
get() = mapAdapter.fromJson(content) ?: emptyMap()

private fun MiPushMessage.extractMetadata(): Map<String, Any> {
return hashMapOf<String, Any>().apply {
put("xiaomi.message_type", messageType)
messageId?.also { put("xiaomi.message_id", it) }
userAccount?.also { put("xiaomi.user_account", it) }
title?.also { put("xiaomi.title", it) }
topic?.also { put("xiaomi.topic", it) }
alias?.also { put("xiaomi.alias", it) }
category?.also { put("xiaomi.category", it) }
description?.also { put("xiaomi.description", it) }
put("xiaomi.is_arrived_message", isArrivedMessage)
put("xiaomi.is_notified", isNotified)
put("xiaomi.notify_type", notifyType)
put("xiaomi.notify_id", notifyId)
put("xiaomi.pass_through", passThrough)
put("xiaomi.extra", extra)
}
}
}

0 comments on commit c884031

Please sign in to comment.