Integration of automatic push notifications, which are delivered in case that user has received a message in a thread while the CXone Chat SDK is not active, is an optional step.
CXone Chat SDK for Android is leveraging Firebase Cloud Messaging to deliver push notifications to the user's device. If the application wishes to receive these push notifications, it has to complete the "Enable Push Notifications" part of CXone Chat SDK Set Up
As outlined in the "Enable Push Notifications" subchapter, the CXone Chat SDK needs a Firebase token for push notification functionality. It can be passed to the SDK continuously, whenever it is updated (preferred variant), or if necessary, only when chat functionality is activated.
Important
Usage of this case study is intended for scenarios where you are not using provided UI module artifact. If you are using the UI module, then the Service part the use-case is already covered by the UI module and the case study is only provided for educational purposes, assuming that your Firebase setup doesn't require more advanced approach.
As outlined in Firebase guide - Set up the SDK application needs a service that will extend FirebaseMessagingService.
In this case study we will call it PushListenerService.
For successful integration of CXone Chat SDK driven push notifications, it has to implement the following functionalities.
- Supply SDK with token updates
- Display notifications received from the backend
Example implementation:
internal class PushListenerService : com.google.firebase.messaging.FirebaseMessagingService() {
private val chatSdkProvider = ChatSdkProvider // We assume that there is a singleton instance of class which can provide an instance of Chat SDK
// 1. Supplying SDK with token updates
override fun onNewToken(token: String) {
super.onNewToken(token)
val chat = chatSdkProvider?.chatInstance
if (chat == null) {
// There is no existing instance of Chat SDK, no need to update.
Log.v(TAG, "No chat instance present, token not passed")
return
}
chat.setDeviceToken(token) // Update the instance with current token
Log.d(TAG, "Registering push notifications token: $token")
}
// 2. Display the notification
override fun onMessageReceived(remoteMessage: RemoteMessage) {
sendNotification(
remoteMessage.data["pinpoint.notification.title"],
remoteMessage.data["pinpoint.notification.body"])
}
private fun sendNotification(title: String?, messageBody: String?) {
val channelId = getString(R.string.default_notification_channel_id) // Id for chat notifications
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = Builder(this, channelId)
.setContentTitle(title)
.setContentText(messageBody)
.setSmallIcon(R.mipmap.ic_launcher) // Application provided icon for chat notifications
.setAutoCancel(false) // This option is up to the application, it is included for completeness
.setSound(defaultSoundUri) // This option is up to the application, it is included for completeness
.setPriority(2) // This option is up to the application, it is included for completeness
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// Since Android Oreo notification channel is needed.
val channel = NotificationChannel(
channelId,
getString(R.string.notification_channel_title), // Application should provide title serving as a description for chat notification channel
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
val notification = notificationBuilder.build()
notificationManager.notify(CHAT_NOTIFICATION_ID, notification)
}
private companion object {
const val CHAT_NOTIFICATION_ID = 0
}
}Integrating application has to provide Chat SDK with the current value of the Firebase messaging token, after SDK is initialized in order for proper push message support.
// context and config are left out for briefness.
// build() is a suspend function — call it from a coroutine.
scope.launch {
val builder = ChatBuilder(context = context, config = socketFactoryConfiguration)
val chat = builder.build()
Firebase.messaging.token.addOnCompleteListener { task ->
chat.setDeviceToken(if (task.isSuccessful) task.result else null)
}
// TODO set chat instance to class property
}It is possible to unregister the device from receiving CXone Chat SDK push messages/notifications
by passing null to the setDeviceToken() method.