com.pubnub.api.models.consumer.push.payload.PushPayloadHelper.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-kotlin Show documentation
Show all versions of pubnub-kotlin Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of
broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter
second!
package com.pubnub.api.models.consumer.push.payload
import com.pubnub.api.enums.PNPushEnvironment
import java.util.Locale
class PushPayloadHelper {
var commonPayload: Map? = null
var fcmPayload: FCMPayload? = null
var mpnsPayload: MPNSPayload? = null
var apnsPayload: APNSPayload? = null
fun build(): Map {
return mutableMapOf().apply {
apnsPayload?.let {
it.toMap().run {
if (isNotEmpty()) {
put("pn_apns", this)
}
}
}
fcmPayload?.let {
it.toMap().run {
if (isNotEmpty()) {
put("pn_gcm", this)
}
}
}
mpnsPayload?.let {
it.toMap().run {
if (isNotEmpty()) {
put("pn_mpns", this)
}
}
}
commonPayload?.let { putAll(it) }
}
}
class APNSPayload : PushPayloadSerializer {
var aps: APS? = null
var apns2Configurations: List? = null
var custom: Map? = null
override fun toMap(): Map {
return mutableMapOf().apply {
aps?.let {
it.toMap().run {
if (this.isNotEmpty()) {
put("aps", this)
}
}
}
apns2Configurations?.let { put("pn_push", it.map { it.toMap() }.filter { it.isNotEmpty() }.toList()) }
custom?.let { putAll(it) }
}
}
class APS : PushPayloadSerializer {
var alert: Any? = null
var badge: Int? = null
var sound: String? = null
override fun toMap(): Map {
return mutableMapOf().apply {
alert?.let { put("alert", it) }
badge?.let { put("badge", it) }
sound?.let { put("sound", it) }
}
}
}
class APNS2Configuration : PushPayloadSerializer {
var collapseId: String? = null
var expiration: String? = null
var targets: List? = null
var version: String? = null
override fun toMap(): Map {
return mutableMapOf().apply {
collapseId?.let { put("collapse_id", it) }
expiration?.let { put("expiration", it) }
targets?.let {
it.map { it.toMap() }.filter { it.isNotEmpty() }.toList().run {
if (this.isNotEmpty()) {
put("targets", this)
}
}
}
version?.let { put("version", it) }
}
}
class Target : PushPayloadSerializer {
var topic: String? = null
var excludeDevices: List? = null
var environment: PNPushEnvironment? = null
override fun toMap(): Map {
return mutableMapOf().apply {
topic?.let { put("topic", it) }
excludeDevices?.let { put("excluded_devices", it) }
environment?.let { put("environment", it.name.lowercase(Locale.getDefault())) }
}
}
}
}
}
class MPNSPayload : PushPayloadSerializer {
var count: Int? = null
var backTitle: String? = null
var title: String? = null
var backContent: String? = null
var type: String? = null
var custom: Map? = null
override fun toMap(): Map {
return mutableMapOf().apply {
count?.let { put("count", it) }
backTitle?.let { put("back_title", it) }
title?.let { put("title", it) }
backContent?.let { put("back_content", it) }
type?.let { put("type", it) }
custom?.let { putAll(it) }
}
}
}
class FCMPayload : PushPayloadSerializer {
var custom: Map? = null
var data: Map? = null
var notification: Notification? = null
override fun toMap(): Map {
return mutableMapOf().apply {
custom?.let { putAll(it) }
data?.let {
if (it.isNotEmpty()) {
put("data", it)
}
}
notification?.let {
it.toMap().run {
if (this.isNotEmpty()) {
put("notification", this)
}
}
}
}
}
class Notification : PushPayloadSerializer {
var title: String? = null
var body: String? = null
var image: String? = null
override fun toMap(): Map {
return mutableMapOf().apply {
title?.let { put("title", it) }
body?.let { put("body", it) }
image?.let { put("image", it) }
}
}
}
}
}