With push notifications, your customers can be alerted about messaging activity even when they aren’t viewing the conversation. When a rep sends a message, your customer receives a notification on their device.
This article applies to the following implementations:
When implementing the onNewToken method, call the CoreClient.provideDeviceToken(applicationContext, token) method. This method should only be called one time per token. See the provideDeviceToken reference documentation.
Pass notification token to SDK
1internal class FCMTokenService : FirebaseMessagingService(){2 private val supervisorJob = SupervisorJob()3 private val scope = CoroutineScope(Dispatchers.Main + supervisorJob)45 override fun onDestroy(){6 super.onDestroy()7 supervisorJob.cancel("Service destroyed")8}910 override fun onNewToken(token: String){11 scope.launch{ CoreClient.provideDeviceToken(applicationContext, token)}12}13}
Handling a Tap (Optional)
By default, when a user taps on the notification, your app launches to the launcher activity (typically this activity is set up as the MainActivity in your app). If you want to take them to the updated conversation, extract the conversation_id property, and then use that value to open the correct messaging conversation.
To handle an incoming notification when the app is in the foreground, implement the onMessageReceived method in the FirebaseMessagingService. The notification can be found in the notification payload of the remoteMessage. To learn more, see Handling messages in the Firebase documentation.
Handle notification in foreground
1class FCMTokenService : FirebaseMessagingService(){2 private val supervisorJob = SupervisorJob()3 private val scope = CoroutineScope(Dispatchers.Main + supervisorJob)45 override fun onDestroy(){6 super.onDestroy()7 supervisorJob.cancel("Service destroyed")8}910 override fun onNewToken(token: String){11 scope.launch{ CoreClient.provideDeviceToken(applicationContext, token)}12}1314 override fun onMessageReceived(remoteMessage: RemoteMessage){1516 // Check if message contains a data payload17 val notificationID = Random().nextInt()18 val conversationID: UUID = UUID.fromString(remoteMessage.data["conversation_id"])19 val messageBody: String? = remoteMessage.notification?.body2021 // Create config22 val coreConfig = CoreConfiguration.fromFile(this, "configFile.json")23 val uiConfig = UIConfiguration(coreConfig, conversationID)2425 // Create intent to open conversation on tap26 val intent = UIClient.Factory.create(uiConfig).createOpenConversationIntent(this)27 val pendingIntent: PendingIntent = TaskStackBuilder.create(this).run{28 addNextIntentWithParentStack(intent)29 getPendingIntent(notificationID, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)30}3132 // Construct notification33 val notification = NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))34 .setSmallIcon(R.drawable.notifications_icon)35 .setContentTitle("Enhanced In-App Chat")36 .setContentText(messageBody)37 .setPriority(NotificationCompat.PRIORITY_MAX)38 .setContentIntent(pendingIntent)39 .setAutoCancel(true)40 .build()4142 // Issue notification43 with(NotificationManagerCompat.from(this)){44 notify(notificationID, notification)45}46}47}
To handle an incoming notification when the app is in the background, capture the extra argument that was passed to the MainActivity as part of the intent.
Handle notification in background
1class MainActivity : AppCompatActivity(){23 override fun onCreate(savedInstanceState: Bundle?){4 super.onCreate(savedInstanceState)56 // The conversationID is delivered in the extras of the intent7 // of the launcher Activity if app is backgrounded8 intent.extras?.getString("conversation_id")?.let{ conversationID ->910 // Create config11 val coreConfig = CoreConfiguration.fromFile(this, "configFile.json")12 val uiConfig = UIConfiguration(coreConfig, UUID.fromString(conversationID))1314 // Start chat activity15 UIClient.Factory.create(uiConfig).openConversationActivity(this)16}17}1819 override fun onNewIntent(intent: Intent?){20 super.onNewIntent(intent)21 setIntent(intent)22}2324 // ...25}