You can then call processFirebaseCloudMessagingData function to handle push notifications, as shown below.
When app is running in the foreground, callback is automatically handled by an already registered ChannelListener.
When app is in the background, app is currently not running and may not have a callback already registered. In that case, you can register an anonymous callback to handle events as shown below:
(processFirebaseCloudMessagingData function accepts boolean value for its last parameter forceCallback. You must set forceCallback to true to receive callbacks from FCM, regardless of whether you are already receiving callbacks from the realtime connection.)
@OverridepublicvoidonMessageReceived(RemoteMessage remoteMessage) {if (remoteMessage.getData().containsKey("talkplus")) {try {// If you need channelId, title and body info, you can retrieve them hereJSONObject talkplus =newJSONObject(remoteMessage.getData().get("talkplus") );String channelId =talkplus.getString("channelId");String messageId =talkplus.getString("messageId"); // available only for message eventString title =talkplus.getString("title");String body =talkplus.getString("body"); } catch (JSONException e) { }if (isBackground) {TalkPlus.processFirebaseCloudMessagingData(remoteMessage.getData(),new TalkPlus.ChannelListener() {// handle push notification @OverridepublicvoidonMemberAdded(TPChannel channel,List<TPUser> users) { } @OverridepublicvoidonMemberLeft(TPChannel channel,List<TPUser> users) { } @OverridepublicvoidonMessageReceived(TPChannel channel,TPMessage message) { } @OverridepublicvoidonChannelChanged(TPChannel channel) { } },true ); } else {TalkPlus.processFirebaseCloudMessagingData(remoteMessage.getData(),true); } }}
overridefunonMessageReceived(remoteMessage: RemoteMessage) { remoteMessage.data["talkplus"]?.let {try {// If you need channelId, title and body info, you can retrieve them hereval talkplus =JSONObject(it)val channelId = talkplus.getString("channelId")val messageId = talkplus.getString("messageId") // available only for message eventval title = talkplus.getString("title")val body = talkplus.getString("body") } catch (e: JSONException) { }if (isBackground()) { TalkPlus.processFirebaseCloudMessagingData( remoteMessage.data,object : TalkPlus.ChannelListener {// handle push notificationoverridefunonMemberAdded(channel: TPChannel, users: List<TPUser>) { }overridefunonMemberLeft(channel: TPChannel, users: List<TPUser>) { }overridefunonMessageReceived(channel: TPChannel, message: TPMessage) { }overridefunonChannelChanged(channel: TPChannel) { } },true ) } else { TalkPlus.processFirebaseCloudMessagingData(remoteMessage.data, true) } }}
In order to have complete control of FCM push notification both in foreground and background, you need to receive data type FCM push notification and handle it via onMessageReceived. To enable this feature, you need to make sure the following settings are in place in dashboard:
Push Notification is enabled
Disable push notification for Android (this allows only data type FCM notifications for Android)