Android Notifications
Set up notification solutions for your Android app
Push Notifications
For push notifications, you must have a Firebase account. If you do not have one already, you can sign up to Firebase console.
Contact Khoros Support to update the GCM/FCM server key.
Firebase Cloud Messaging (FCM) is already enabled in my app
If you are already using Firebase in your application, follow these steps:
- In your
FirebaseMessagingService
onNewToken(String registrationId)
method get the FCM Registration token and pass it to the Brand Messenger SDK method:
@Override
public void onNewToken(String registrationId) {
super.onNewToken(registrationId);
Log.i(TAG, "Found Registration Id:" + registrationId);
KBMFirebaseMessagingService.setNewToken(registrationId, this);
}
override fun onNewToken(registrationId: String) {
super.onNewToken(registrationId)
KBMFirebaseMessagingService.setNewToken(registrationId, context)
}
- Add the code below in
BrandMessengerManager.login
onSuccess()
method [Refer here].
if (BrandMessengerUserPreference.getInstance(context).isRegistered()) {
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
String token = task.getResult();
BrandMessenger.registerForPushNotification(context, token, new KBMPushNotificationHandler() {
@Override
public void onSuccess(RegistrationResponse registrationResponse) {
}
@Override
public void onFailure(RegistrationResponse registrationResponse, Exception exception) {
}
});
}
});
}
if (BrandMessengerUserPreference.getInstance(context).isRegistered) {
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(object : OnCompleteListener<String?> {
override fun onComplete(task: Task<String?>) {
val token: String? = task.getResult()
BrandMessenger.registerForPushNotification(
context,
token,
object : KBMPushNotificationHandler {
override fun onSuccess(registrationResponse: RegistrationResponse) {}
override fun onFailure(
registrationResponse: RegistrationResponse,
exception: Exception
) {
}
})
}
})
}
- For Receiving Notifications in FCM Add the following in your
FirebaseMessagingService
ononMessageReceived(RemoteMessage remoteMessage)
method:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "Message data:" + remoteMessage.getData());
KBMFirebaseMessagingService.triggerBrandMessengerNotification(message, this);
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.i(TAG, "Message data:" + remoteMessage.data)
KBMFirebaseMessagingService.triggerBrandMessengerNotification(message, context)
}
Setup FCM push notification code in your android app
If you already have a Firebase account and code to retrieve the firebase registration token for the client app instance, skip this step.
-
If you haven't already, add Firebase to your Android project.
-
Once you're done with the above setup, retrieve the FCM server key from Firebase by selecting Settings icon > Project settings.

- Once you clicked project settings select the Cloud Messaging tab under Settings. In the Project Credentials, copy your Server Key, which is highlighted in blue.

- Update the FCM server key in Brand Messenger.
Contact Khoros Support with your FCM server key to update it in Brand Messenger.
- In case you don't have the existing FCM related code follow this:
Edit your app manifest by adding the following to your app's AndroidManifest
file:
<service android:name="com.brandmessenger.core.KBMFirebaseMessagingService" android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
- To Update token to Brand Messenger server, Setup the
registerForPushNotification task
by adding the below lines of code inonSuccess()
callback method ofBrandMessengerManager.login
[Refer here]:
if (BrandMessengerUserPreference.getInstance(context).isRegistered()) {
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
String token = task.getResult();
BrandMessenger.registerForPushNotification(context, token, new KBMPushNotificationHandler() {
@Override
public void onSuccess(RegistrationResponse registrationResponse) {
}
@Override
public void onFailure(RegistrationResponse registrationResponse, Exception exception) {
}
});
}
});
}
if (BrandMessengerUserPreference.getInstance(context).isRegistered) {
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(object : OnCompleteListener<String?> {
override fun onComplete(task: Task<String?>) {
val token: String? = task.getResult()
BrandMessenger.registerForPushNotification(
context,
token,
object : KBMPushNotificationHandler {
override fun onSuccess(registrationResponse: RegistrationResponse) {}
override fun onFailure(
registrationResponse: RegistrationResponse,
exception: Exception
) {
}
})
}
})
}
Notification click handling and passing data to your activity
- Brand Messenger can handle the showing of chat notifications and it will pass the data to your activity once you click on the notification to open.
Add the below metadata in your app Android Manifest file
and pass your activity package name with an activity name that activity will receive the data of the message object
<meta-data android:name="activity.open.on.notification"
android:value="<YOUR_ACTIVITY_FULL_PACKAGE_NAME_WITH_ACTIVITY_NAME>" /> //Change the value to your activity package
Note: Change the above metadata value to your activity package name
For getting data in your activity based on intent
Intent intent = getIntent();
if (intent != null){
String messageJson = intent.getStringExtra(BrandMessengerConstants.MESSAGE_JSON_INTENT);
if (!TextUtils.isEmpty(messageJson)) {
Message message = (Message) GsonUtils.getObjectFromJson(messageJson, Message.class);
}
}
val intent = intent
if (intent != null) {
val messageJson = intent.getStringExtra(MobiComKitConstants.MESSAGE_JSON_INTENT)
if (!TextUtils.isEmpty(messageJson)) {
val message = GsonUtils.getObjectFromJson(messageJson, Message::class.java) as Message
}
}
Sync messages
Whenever the connectivity changes, for example the status of the internet connection, use call the below method to sync messages from the server.
SyncCallService.getInstance(this).syncMessages(null);
SyncCallService.getInstance(this).syncMessages(null)
Updated 9 months ago