Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify custom data in Push payloads #176

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pubnub-chat-api/api/pubnub-chat-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -236,6 +238,7 @@ interface Channel {
quotedMessage: Message? = null,
files: List<InputFile>? = null,
usersToMention: Collection<String>? = null,
customPushData: Map<String, String>? = null
): PNFuture<PNPublishResult>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ abstract class BaseChannel<C : Channel, M : Message>(
quotedMessage: Message?,
files: List<InputFile>?,
usersToMention: Collection<String>?,
customPushData: Map<String, String>?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General question to this feature. How would you test it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the push data is merged into the message content, so I would test that the message was sent (possibly with a mock of the publish PN SDK function) with the correct data in the correct place in the FCM and APNS payloads

): PNFuture<PNPublishResult> {
return sendTextInternal(
text = text,
Expand All @@ -289,7 +290,8 @@ abstract class BaseChannel<C : Channel, M : Message>(
ttl = ttl,
quotedMessage = quotedMessage,
files = files,
usersToMention = usersToMention
usersToMention = usersToMention,
customPushData = customPushData,
)
}

Expand All @@ -301,7 +303,8 @@ abstract class BaseChannel<C : Channel, M : Message>(
ttl: Int?,
quotedMessage: Message?,
files: List<InputFile>?,
usersToMention: Collection<String>? = null
usersToMention: Collection<String>? = null,
customPushData: Map<String, String>? = null,
): PNFuture<PNPublishResult> {
if (quotedMessage != null && quotedMessage.channelId != id) {
return log.logErrorAndReturnException(CANNOT_QUOTE_MESSAGE_FROM_OTHER_CHANNELS).asFuture()
Expand All @@ -313,7 +316,7 @@ abstract class BaseChannel<C : Channel, M : Message>(
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,
Expand All @@ -322,7 +325,7 @@ abstract class BaseChannel<C : Channel, M : Message>(
)
}.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() }
}
Expand Down Expand Up @@ -765,11 +768,12 @@ abstract class BaseChannel<C : Channel, M : Message>(
userId: String,
timetoken: Long,
text: String,
customPushData: Map<String, String>? = null,
): PNFuture<PNPublishResult> {
return chat.emitEvent(
userId,
EventContent.Mention(timetoken, id),
getPushPayload(this, text, chat.config.pushNotifications)
getPushPayload(this, text, chat.config.pushNotifications, customPushData)
)
}

Expand Down Expand Up @@ -888,7 +892,8 @@ abstract class BaseChannel<C : Channel, M : Message>(
internal fun getPushPayload(
baseChannel: BaseChannel<*, *>,
text: String,
pushConfig: PushNotificationsConfig
pushConfig: PushNotificationsConfig,
customPushData: Map<String, String>? = null
): Map<String, Any> {
val apnsTopic = pushConfig.apnsTopic
val apnsEnv = pushConfig.apnsEnvironment
Expand All @@ -904,6 +909,7 @@ abstract class BaseChannel<C : Channel, M : Message>(
}
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 {
Expand Down Expand Up @@ -932,7 +938,10 @@ abstract class BaseChannel<C : Channel, M : Message>(
)
}
)
baseChannel.name?.let { custom = mapOf("subtitle" to it) }
custom = buildMap {
baseChannel.name?.let { put("subtitle", it) }
customPushData?.let { putAll(it) }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ data class ThreadChannelImpl(
quotedMessage: Message?,
files: List<InputFile>?,
usersToMention: Collection<String>?,
customPushData: Map<String, String>?,
): PNFuture<PNPublishResult> {
return createThreadAndSend {
super.sendText(
Expand All @@ -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<PNPublishResult> {
override fun emitUserMention(
userId: String,
timetoken: Long,
text: String,
customPushData: Map<String, String>?,
): PNFuture<PNPublishResult> {
return chat.emitEvent(
userId,
EventContent.Mention(timetoken, id, parentChannelId),
getPushPayload(this, text, chat.config.pushNotifications)
getPushPayload(this, text, chat.config.pushNotifications, customPushData)
)
}

Expand Down
Loading