Business Messaging Client Side v2
Business Messaging
Version 2

Track notification events

Copy link

Sendbird Business Messaging offers comprehensive event tracking capabilities for all channels. The purpose of this feature is to provide insights into how users interact with notifications and messages. By tracking events such as viewed, clicked, and delivered, you can analyze user behavior and optimize your messaging strategy.

Event tracking are essential for some of Business Messaging's features, such as:

  • Sequence: Send a message through various channels based on the event.
  • User insights: Monitor message's status through the events per user.
  • Analytics: Analyze the channel like Click rates, opened rates, etc.

Events

Copy link

You can track the following events for each channel:

  • sent: When a message is sent to the channel.

  • delivered: When a message is delivered to the user. For a push notification, call the SendbirdPushHelper.markPushNotificationAsDelivered method to track this event. For a WhatsApp channel, the delivered event is automatically handled by Sendbird.

  • viewed: When a user views the message in the channel. If a message is not rendered in the current screen, the viewed event won't be called. For an in-app message, the viewed event is automatically handled by the UIKit.

  • clicked: When a user clicks a button on the message. For an in-app message, the clicked event is automatically handled by the UIKit. For a push notification, call the SendbirdPushHelper.markPushNotificationAsClicked method to track this event.

Events
ChannelsSentDeliveredViewedClicked

In-app

N/A

N/A

Push notification

N/A

N/A

WhatsApp

N/A


N/A

N/A

Kakao Alim Talk

N/A

N/A


Push notifications

Copy link

For the Business Messaging to accurately track push notification events, push notifications must be correctly set up in your mobile application. Sendbird provides detailed guides for configuring push notifications on both Android and iOS.

Set up environment for Android

Copy link

To integrate push notifications with Sendbird on Android, follow the guide for setting up push notifications for Firebase Cloud Messaging (FCM). This setup includes creating a Firebase project, configuring your app with Firebase, adding Firebase configuration files to your project, and integrating FCM with the Sendbird SDK.

For a step-by-step guide, visit the Sendbird documentation: Push notifications for UIKit.

This guide will walk you through the steps to ensure your Android application can receive and process push notifications, pivotal for tracking delivered and clicked events within your app.

Track delivered

Copy link

If you've implemented SendbirdPushHelper and registered message events in SendbirdPushHandler during the push notification setup, a delivered event is automatically tracked when a user receives a push notification.

Track clicked

Copy link

Similar to in-app messages, tracking when a push notification is clicked provides insights into how users interact with notifications and which messages prompt action. Call the methods below when a push notification banner is clicked.

The following two code snippets demonstrate the implementation for event tracking.

Ship the data to the pending intent of the notification

Copy link
class YourFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        // create redirect target Intent
        // the remoteMessage is the value passed to the onMessageReceived function.
        val remoteMessageData = HashMap(remoteMessage.data)
        val targetIntent = Intent(context, TARGET_INTENT::class.java).apply {
            // set other values..
            putExtra("PUSH_NOTIFICATION_DATA", remoteMessageData)
        }

        // create target pending Intent to launch when the notification is clicked.
        @SuppressLint("UnspecifiedImmutableFlag")
        val pending intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PendingIntent.getActivity(
                context,
                YOUR_REQUEST_CODE,
                targetIntent,
                PendingIntent.FLAG_IMMUTABLE
            )
        } else {
            PendingIntent.getActivity(context, messageId.toInt(), intent, 0)
        }

        // Fill the `tag`, `channelId`, `channelName`, and `notificationId`
        val tag = "NOTIFICATION TAG"
        val channelId = "YOUR CHANNEL ID"
        val channelName = "YOUR CHANNEL NAME"
        val notificationId = YOUR_NOTIFICATION_ID
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH).let {
                notificationManager.createNotificationChannel(it)
            }
        }

        // send notification
        val notificationBuilder = NotificationCompat.Builder(context, channelId)
            // .. other settings
            .setContentIntent(pendingIntent)
        notificationManager.notify(tag, notificationId, notificationBuilder.build())
    }
}

Call the markPushNotificationAsClicked method

Copy link

You can call the markPushNotificationAsClicked method in the onNewIntent method of the activity with deduplication logic.

class YourActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // .. set other configuration
        sendMarkNotificationAsClicked(intent)
    }
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        sendMarkNotificationAsClicked(intent)
    }

    // This key must match with the extra key of Notification pending intent
    private val PUSH_NOTIFICATION_DATA = "PUSH_NOTIFICATION_DATA"
    private fun sendMarkNotificationAsClicked(intent: Intent?) {
        intent?.let {
            if (intent.hasExtra(PUSH_NOTIFICATION_DATA)) {
                intent.getSerializable(PUSH_NOTIFICATION_DATA, HashMap::class.java)?.let { hashMap ->
                    val data: Map<String, String> = hashMap.mapNotNull {
                        if (it.key is String && it.value is String) {
                            it.key as String to it.value as String
                        } else {
                            null
                        }
                    }.toMap()
                    SendbirdPushHelper.markPushNotificationAsClicked(data)
                }
                // You must delete it after use so that it is not reused on the next run.
                intent.removeExtra(PUSH_NOTIFICATION_DATA)
            }
        }
    }
}

Set up environment for iOS

Copy link

For iOS, setting up push notifications involves registering your app with the Apple Push Notification service (APNs), configuring your app's notification settings, and integrating these settings with the Sendbird SDK. This process ensures your iOS app is prepared to handle push notifications, allowing for effective tracking within the Business Messaging feature.

Detailed instructions can be found in the Sendbird documentation: Push notifications for UIKit.

This comprehensive guide provides the steps to configure your iOS application to receive push notifications from APNs and how to communicate these notifications to the Sendbird servers for tracking purposes.

Track delivered

Copy link

To mark a push notification as delivered when received, follow the instructions below:

  1. Be sure to set the app group in AppDelegate when initializing the SendbirdChat SDK.

  2. Implement the necessary method in the NotificationServiceExtension.

class NotificationService: UNNotificationServiceExtension {
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    
    override func didReceive(
        _ request: UNNotificationRequest, 
        withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
    ) {
        self.bestAttemptContent = (request.content.mutableCopy()
              as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            SendbirdChat.setAppGroup("YOUR_APP_ID")
            SendbirdChat.markPushNotificationAsDelivered(
                remoteNotificationPayload: bestAttemptContent.userInfo
            ) { error in
                
            }
           
            // Always call the completion handler when done.
            contentHandler(bestAttemptContent)
        }    
    }
    // ...
}

Note: Sendbird SDK marks a push notification as delivered when receiving it from AppDelegate regardless of whether the client app is in the foreground or background. This ensures that the notification is delivered even if a user does't see the push banner. If your client app can receive a push notification in the foreground but doesn't want to display it, add a condition called markPushNotificationAsDelivered method so that it does not deliver in the foreground.

Track clicked

Copy link

Similar to in-app messages, tracking when a push notification is clicked provides insights into how users interact with notifications and which messages prompt action. Implement the userNotificationCenter (_:didReceive:withCompletionHandler:) method in AppDelegate. This is called when a user clicks on a push notification.

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    // ...
    public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                       didReceive response: UNNotificationResponse,
                                       withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
        let userInfo = response.notification.request.content.userInfo

        SendbirdChat.markPushNotificationAsClicked(remoteNotificationPayload: userInfo)
        // ...
    }
    // ...
}