You can set whether you want to receive push notifications from specific channels.
// Enable Push Notification from the channel
TalkPlus.enableChannelPushNotification(String channel, new TalkPlus.CallbackListener<TPUser>() {
@Override
public void onSuccess(TPUser tpUser) {
}
@Override
public void onFailure(int i, Exception e) {
}
});
// Disable Push Notification from the channel
TalkPlus.disableChannelPushNotification(String channel, new TalkPlus.CallbackListener<TPUser>() {
@Override
public void onSuccess(TPUser tpUser) {
}
@Override
public void onFailure(int i, Exception e) {
}
});
// Enable Push Notification from the channel
TalkPlus.enableChannelPushNotification(channel: String, object : TalkPlus.CallbackListener<TPUser>() {
override fun onSuccess(tpUser: TPUser) { }
override fun onFailure(i: Int, e: Exception) { }
}
// Disable Push Notification from the channel
TalkPlus.disableChannelPushNotification(channel: String, object : TalkPlus.CallbackListener<TPUser>() {
override fun onSuccess(tpUser: TPUser) { }
override fun onFailure(i: Int, e: Exception) { }
})
Push Notification
Push Notification is handled by FCM.
First, integrate FCM on TalkPlus dashboard. Then, when a user logs in, call registerFCMToken function to register FCM token from that device.
The following code demonstrates how you can obtain and register an FCM token from FirebaseMessaging.
// request push notification permission for Android 13 (API 33) and above
private ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
// permission granted
} else {
// permission refused
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
// already have permission
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
// show permission request information to user
// request permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
} else {
// request permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
}
// obtain token from Firebase Messaging
public void getFCMToken() {
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull com.google.android.gms.tasks.Task<String> task) {
if (!task.isSuccessful()) {
return;
}
String fcmToken = task.getResult();
// register token
TalkPlus.registerFCMToken(fcmToken, new CallbackListener<Void>() {
@Override
public void onSuccess(Void result) { }
@Override
public void onFailure(int errorCode, Exception exception) { }
});
}
});
}
// request push notification permission for Android 13 (API 33) and above
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
// permission granted
} else {
// permission refused
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
when {
ContextCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED -> {
// already have permission
}
shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
// show permission request information to user
// request permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
else -> {
// request permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}
// obtain token from Firebase Messaging
fun getFCMToken() {
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
return@OnCompleteListener
}
val fcmToken: String = task.result
// register token
TalkPlus.registerFCMToken(fcmToken, object : CallbackListener<Void?> {
override fun onSuccess(t: Void?) { }
override fun onFailure(errorCode: Int, exception: Exception) { }
})
return@OnCompleteListener
})
}
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.)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().containsKey("talkplus")) {
try {
// If you need channelId, title and body info, you can retrieve them here
JSONObject talkplus = new JSONObject(
remoteMessage.getData().get("talkplus")
);
String channelId = talkplus.getString("channelId");
String messageId = talkplus.getString("messageId"); // available only for message event
String title = talkplus.getString("title");
String body = talkplus.getString("body");
} catch (JSONException e) {
}
if (isBackground) {
TalkPlus.processFirebaseCloudMessagingData(
remoteMessage.getData(),
new TalkPlus.ChannelListener() {
// handle push notification
@Override
public void onMemberAdded(TPChannel channel, List<TPUser> users) {
}
@Override
public void onMemberLeft(TPChannel channel, List<TPUser> users) {
}
@Override
public void onMessageReceived(TPChannel channel, TPMessage message) {
}
@Override
public void onChannelChanged(TPChannel channel) {
}
},
true
);
} else {
TalkPlus.processFirebaseCloudMessagingData(remoteMessage.getData(), true);
}
}
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage.data["talkplus"]?.let {
try {
// If you need channelId, title and body info, you can retrieve them here
val talkplus = JSONObject(it)
val channelId = talkplus.getString("channelId")
val messageId = talkplus.getString("messageId") // available only for message event
val title = talkplus.getString("title")
val body = talkplus.getString("body")
} catch (e: JSONException) {
}
if (isBackground()) {
TalkPlus.processFirebaseCloudMessagingData(
remoteMessage.data,
object : TalkPlus.ChannelListener {
// handle push notification
override fun onMemberAdded(channel: TPChannel, users: List<TPUser>) { }
override fun onMemberLeft(channel: TPChannel, users: List<TPUser>) { }
override fun onMessageReceived(channel: TPChannel, message: TPMessage) { }
override fun onChannelChanged(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)