From f44bfba1b63ebfa7f5ff28db9382148d8f6c0915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Kalici=C5=84ski?= Date: Wed, 26 Feb 2025 13:05:15 +0100 Subject: [PATCH] Add ability to specify custom data in Push payloads --- pubnub-chat-api/api/pubnub-chat-api.api | 4 ++-- .../kotlin/com/pubnub/chat/Channel.kt | 3 +++ .../chat/internal/channel/BaseChannel.kt | 23 +++++++++++++------ .../internal/channel/ThreadChannelImpl.kt | 13 ++++++++--- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/pubnub-chat-api/api/pubnub-chat-api.api b/pubnub-chat-api/api/pubnub-chat-api.api index 5cde03d6..ced35704 100644 --- a/pubnub-chat-api/api/pubnub-chat-api.api +++ b/pubnub-chat-api/api/pubnub-chat-api.api @@ -38,9 +38,9 @@ public abstract interface class com/pubnub/chat/Channel { public abstract fun pinMessage (Lcom/pubnub/chat/Message;)Lcom/pubnub/kmp/PNFuture; public abstract fun plus (Lcom/pubnub/api/models/consumer/objects/channel/PNChannelMetadata;)Lcom/pubnub/chat/Channel; public abstract fun registerForPush ()Lcom/pubnub/kmp/PNFuture; - public abstract fun sendText (Ljava/lang/String;Ljava/util/Map;ZZLjava/lang/Integer;Lcom/pubnub/chat/Message;Ljava/util/List;Ljava/util/Collection;)Lcom/pubnub/kmp/PNFuture; + public abstract fun sendText (Ljava/lang/String;Ljava/util/Map;ZZLjava/lang/Integer;Lcom/pubnub/chat/Message;Ljava/util/List;Ljava/util/Collection;Ljava/util/Map;)Lcom/pubnub/kmp/PNFuture; public abstract fun sendText (Ljava/lang/String;Ljava/util/Map;ZZLjava/lang/Integer;Ljava/util/Map;Ljava/util/Map;Ljava/util/List;Lcom/pubnub/chat/Message;Ljava/util/List;)Lcom/pubnub/kmp/PNFuture; - public static synthetic fun sendText$default (Lcom/pubnub/chat/Channel;Ljava/lang/String;Ljava/util/Map;ZZLjava/lang/Integer;Lcom/pubnub/chat/Message;Ljava/util/List;Ljava/util/Collection;ILjava/lang/Object;)Lcom/pubnub/kmp/PNFuture; + public static synthetic fun sendText$default (Lcom/pubnub/chat/Channel;Ljava/lang/String;Ljava/util/Map;ZZLjava/lang/Integer;Lcom/pubnub/chat/Message;Ljava/util/List;Ljava/util/Collection;Ljava/util/Map;ILjava/lang/Object;)Lcom/pubnub/kmp/PNFuture; public static synthetic fun sendText$default (Lcom/pubnub/chat/Channel;Ljava/lang/String;Ljava/util/Map;ZZLjava/lang/Integer;Ljava/util/Map;Ljava/util/Map;Ljava/util/List;Lcom/pubnub/chat/Message;Ljava/util/List;ILjava/lang/Object;)Lcom/pubnub/kmp/PNFuture; public abstract fun setRestrictions (Lcom/pubnub/chat/User;ZZLjava/lang/String;)Lcom/pubnub/kmp/PNFuture; public static synthetic fun setRestrictions$default (Lcom/pubnub/chat/Channel;Lcom/pubnub/chat/User;ZZLjava/lang/String;ILjava/lang/Object;)Lcom/pubnub/kmp/PNFuture; diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt index 60b85b62..5c0a46a5 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt @@ -224,6 +224,8 @@ interface Channel { * original message content, and userId as the identifier of the user who published the quoted message. * @param files One or multiple files attached to the text message. * @param usersToMention A collection of user ids to automatically notify with a mention after this message is sent. + * @param customPushData Additional key-value pairs that will be added to the FCM and/or APNS push messages for the + * message itself and any user mentions. * * @return [PNFuture] containing [PNPublishResult] that holds the timetoken of the sent message. */ @@ -236,6 +238,7 @@ interface Channel { quotedMessage: Message? = null, files: List? = null, usersToMention: Collection? = null, + customPushData: Map? = null ): PNFuture /** diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt index e04c4031..97576634 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt @@ -280,6 +280,7 @@ abstract class BaseChannel( quotedMessage: Message?, files: List?, usersToMention: Collection?, + customPushData: Map?, ): PNFuture { return sendTextInternal( text = text, @@ -289,7 +290,8 @@ abstract class BaseChannel( ttl = ttl, quotedMessage = quotedMessage, files = files, - usersToMention = usersToMention + usersToMention = usersToMention, + customPushData = customPushData, ) } @@ -301,7 +303,8 @@ abstract class BaseChannel( ttl: Int?, quotedMessage: Message?, files: List?, - usersToMention: Collection? = null + usersToMention: Collection? = null, + customPushData: Map? = null, ): PNFuture { if (quotedMessage != null && quotedMessage.channelId != id) { return log.logErrorAndReturnException(CANNOT_QUOTE_MESSAGE_FROM_OTHER_CHANNELS).asFuture() @@ -313,7 +316,7 @@ abstract class BaseChannel( message = EventContent.TextMessageContent(text, filesData).encodeForSending( id, chat.config.customPayloads?.getMessagePublishBody, - getPushPayload(this, text, chat.config.pushNotifications) + getPushPayload(this, text, chat.config.pushNotifications, customPushData) ), meta = meta, shouldStore = shouldStore, @@ -322,7 +325,7 @@ abstract class BaseChannel( ) }.then { publishResult: PNPublishResult -> usersToMention?.forEach { mentionedUser -> - emitUserMention(mentionedUser, publishResult.timetoken, text).async { + emitUserMention(mentionedUser, publishResult.timetoken, text, customPushData).async { it.onFailure { ex -> log.w(throwable = ex) { ex.message.orEmpty() } } @@ -765,11 +768,12 @@ abstract class BaseChannel( userId: String, timetoken: Long, text: String, + customPushData: Map? = null, ): PNFuture { return chat.emitEvent( userId, EventContent.Mention(timetoken, id), - getPushPayload(this, text, chat.config.pushNotifications) + getPushPayload(this, text, chat.config.pushNotifications, customPushData) ) } @@ -888,7 +892,8 @@ abstract class BaseChannel( internal fun getPushPayload( baseChannel: BaseChannel<*, *>, text: String, - pushConfig: PushNotificationsConfig + pushConfig: PushNotificationsConfig, + customPushData: Map? = null ): Map { val apnsTopic = pushConfig.apnsTopic val apnsEnv = pushConfig.apnsEnvironment @@ -904,6 +909,7 @@ abstract class BaseChannel( } data = buildMap { baseChannel.name?.let { put("subtitle", it) } + customPushData?.let { putAll(it) } } this.android = PushPayloadHelper.FCMPayloadV2.AndroidConfig().apply { this.notification = PushPayloadHelper.FCMPayloadV2.AndroidConfig.AndroidNotification().apply { @@ -932,7 +938,10 @@ abstract class BaseChannel( ) } ) - baseChannel.name?.let { custom = mapOf("subtitle" to it) } + custom = buildMap { + baseChannel.name?.let { put("subtitle", it) } + customPushData?.let { putAll(it) } + } } } diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/ThreadChannelImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/ThreadChannelImpl.kt index 87a87198..c79e963b 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/ThreadChannelImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/ThreadChannelImpl.kt @@ -140,6 +140,7 @@ data class ThreadChannelImpl( quotedMessage: Message?, files: List?, usersToMention: Collection?, + customPushData: Map?, ): PNFuture { return createThreadAndSend { super.sendText( @@ -150,18 +151,24 @@ data class ThreadChannelImpl( ttl = ttl, quotedMessage = quotedMessage, files = files, - usersToMention = usersToMention + usersToMention = usersToMention, + customPushData = customPushData, ) } } override fun copyWithStatusDeleted(): ThreadChannel = copy(status = DELETED) - override fun emitUserMention(userId: String, timetoken: Long, text: String): PNFuture { + override fun emitUserMention( + userId: String, + timetoken: Long, + text: String, + customPushData: Map?, + ): PNFuture { return chat.emitEvent( userId, EventContent.Mention(timetoken, id, parentChannelId), - getPushPayload(this, text, chat.config.pushNotifications) + getPushPayload(this, text, chat.config.pushNotifications, customPushData) ) }