Overriding the Push Notification Handler

In some cases, your app may already use push notifications. In that case, it's useful to be able to take control of the way the Chat SDK handles push notifications.

The first step is to disable the Chat SDK push handling.

ChatSDK.config().setInboundPushHandlingEnabled(false)

In your broadcast receiver make a new instance of the BaseBroadcastHandler:

BaseBroadcastHandler bbc = new BaseBroadcastHandler()

Then in your BroadcastReceiver, onReceive method you need to check to see if the push should be handled by the Chat SDK.

You can do that by checking if the following keys exist:

public void onReceive(Context context, Intent intent) {

    Bundle extras = intent.getExtras();

    final String threadEntityID = extras.getString(Keys.PushKeyThreadEntityID);
    final String userEntityID = extras.getString(Keys.PushKeyUserEntityID);

    if (!StringChecker.isNullOrEmpty(threadEntityID) && !StringChecker.isNullOrEmpty(userEntityID)) {
       // Valid Chat SDK push, send it to the Chat SDK
        bbc.onReceive(context, intent);
    } else {
        // Handle the push yourself
    }
}

Local Push Notifications

Local notifications will be shown if the config flag is set to true:

ChatSDK.config().setShowLocalNotifications(true);

However, what would happen if you're on the ChatActivity and a messages is received for that thread. You don't want the local notification to show! So we have a second layer of control in the form of the local push handler.

ChatSDK.ui().setLocalNotificationHandler(thread -> {
    return false;
});

When deciding whether to show a local push, first the Chat SDK will check to see if this push handler is defined, if it is, it will use that value. It not, it will default back to using the showLocalNotifications value.

As an example, in the ChatActivity there is the following code in onResume:

ChatSDK.ui().setLocalNotificationHandler(thread -> !thread.getEntityID().equals(this.thread.getEntityID()));

This says that we only show a local notification if the thread for the notification is different to the thread currently displayed in the view.

So if you are customizing or building your own main activity, you will need to add some logic into the onResume to decide when pushes should be shown.

The most basic implementation would be:

ChatSDK.ui().setLocalNotificationHandler(thread -> {
    return hatSDK.config().showLocalNotifications;
});

Last updated