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.
1
ChatSDK.config().setInboundPushHandlingEnabled(false)
Copied!
In your broadcast receiver make a new instance of the BaseBroadcastHandler:
1
BaseBroadcastHandler bbc = new BaseBroadcastHandler()
Copied!
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:
1
public void onReceive(Context context, Intent intent) {
2
​
3
Bundle extras = intent.getExtras();
4
​
5
final String threadEntityID = extras.getString(Keys.PushKeyThreadEntityID);
6
final String userEntityID = extras.getString(Keys.PushKeyUserEntityID);
7
​
8
if (!StringChecker.isNullOrEmpty(threadEntityID) && !StringChecker.isNullOrEmpty(userEntityID)) {
9
// Valid Chat SDK push, send it to the Chat SDK
10
bbc.onReceive(context, intent);
11
} else {
12
// Handle the push yourself
13
}
14
}
Copied!

Local Push Notifications

Local notifications will be shown if the config flag is set to true:
1
ChatSDK.config().setShowLocalNotifications(true);
Copied!
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.
1
ChatSDK.ui().setLocalNotificationHandler(thread -> {
2
return false;
3
});
Copied!
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:
1
ChatSDK.ui().setLocalNotificationHandler(thread -> !thread.getEntityID().equals(this.thread.getEntityID()));
Copied!
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:
1
ChatSDK.ui().setLocalNotificationHandler(thread -> {
2
return hatSDK.config().showLocalNotifications;
3
});
Copied!
Last modified 9mo ago
Copy link