com.pubnub.api.endpoints.push.RemoveChannelsFromPush.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.endpoints.push
import com.pubnub.api.Endpoint
import com.pubnub.api.PubNub
import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.enums.PNPushEnvironment
import com.pubnub.api.enums.PNPushType
import com.pubnub.api.models.consumer.push.PNPushRemoveChannelResult
import com.pubnub.api.toCsv
import retrofit2.Call
import retrofit2.Response
import java.util.HashMap
import java.util.Locale
/**
* @see [PubNub.removePushNotificationsFromChannels]
*/
class RemoveChannelsFromPush internal constructor(
pubnub: PubNub,
val pushType: PNPushType,
val channels: List,
val deviceId: String,
val topic: String? = null,
val environment: PNPushEnvironment = PNPushEnvironment.DEVELOPMENT
) : Endpoint(pubnub) {
override fun getAffectedChannels() = channels
override fun validateParams() {
super.validateParams()
if (deviceId.isBlank()) throw PubNubException(PubNubError.DEVICE_ID_MISSING)
if (channels.isEmpty()) throw PubNubException(PubNubError.CHANNEL_MISSING)
if (pushType == PNPushType.APNS2 && topic.isNullOrBlank()) throw PubNubException(PubNubError.PUSH_TOPIC_MISSING)
}
override fun doWork(queryParams: HashMap): Call {
addQueryParams(queryParams)
return if (pushType != PNPushType.APNS2)
pubnub.retrofitManager.pushService
.modifyChannelsForDevice(
subKey = pubnub.configuration.subscribeKey,
pushToken = deviceId,
options = queryParams
)
else
pubnub.retrofitManager.pushService
.modifyChannelsForDeviceApns2(
subKey = pubnub.configuration.subscribeKey,
deviceApns2 = deviceId,
options = queryParams
)
}
override fun createResponse(input: Response): PNPushRemoveChannelResult =
PNPushRemoveChannelResult()
override fun operationType() = PNOperationType.PNRemovePushNotificationsFromChannelsOperation
private fun addQueryParams(queryParams: MutableMap) {
queryParams["remove"] = channels.toCsv()
if (pushType != PNPushType.APNS2) {
queryParams["type"] = pushType.toParamString()
return
}
queryParams["environment"] = environment.name.lowercase(Locale.getDefault())
topic?.run { queryParams["topic"] = this }
}
}