Android
Firebase Cloud Messaging (FCM) Push Notification Setup
Drop-in UI
For push notifications, you must have a Firebase account. Sign up to Firebase console
Go to Applozic Dashboard Push Notification section and 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 the below steps:
- In your
FcmListenerService
onNewToken(String registrationId) ` method get FCM Registration token and pass it to Applozic SDK method :
@Override
public void onNewToken(String registrationId) {
super.onNewToken(registrationId);
Log.i(TAG, "Found Registration Id:" + registrationId);
if (MobiComUserPreference.getInstance(this).isRegistered()) {
try {
RegistrationResponse registrationResponse = new RegisterUserClientService(this).updatePushNotificationId(registrationId);
} catch (Exception e) {
e.printStackTrace();
}
}
}
override fun onNewToken(registrationId: String) {
super.onNewToken(registrationId)
// Applozic token update method you can copy paste this in your file
if (MobiComUserPreference.getInstance(this).isRegistered) {
try {
RegisterUserClientService(this).updatePushNotificationId(registrationId)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
- Add the below code in
Applozic.connectUser
onSuccess()
method [Refer here]
if (MobiComUserPreference.getInstance(this).isRegistered()) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w("FCM", "getInstanceId failed:", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.i("FCM", "Found token :" + token);
Applozic.registerForPushNotification(context, token, new AlPushNotificationHandler() {
@Override
public void onSuccess(RegistrationResponse registrationResponse) {
}
@Override
public void onFailure(RegistrationResponse registrationResponse, Exception exception) {
}
});
}
});
}
FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w("FCM", "getInstanceId failed:", task.exception)
[email protected]
}
val token = task.result?.token
Log.i("FCM", "Found token :$token")
if (token == null) {
Log.i("FCM", "FCM token is null returning")
[email protected]
}
Applozic.registerForPushNotification(
context,
token,
object : AlPushNotificationHandler {
override fun onSuccess(registrationResponse: RegistrationResponse?) {
Log.i("FCM", "Token updated to applozic server")
}
override fun onFailure(
registrationResponse: RegistrationResponse?,
exception: java.lang.Exception?
) {
}
})
})
- For Receiving Notifications in FCM Add the following in your
FcmListenerService
ononMessageReceived(RemoteMessage remoteMessage)
method:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "Message data:" + remoteMessage.getData());
if (remoteMessage.getData().size() > 0) {
if (MobiComPushReceiver.isMobiComPushNotification(remoteMessage.getData())) {
Log.i(TAG, "Applozic notification processing...");
MobiComPushReceiver.processMessageAsync(this, remoteMessage.getData());
return;
}
}
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.i(TAG, "Message data:" + remoteMessage.data)
if (remoteMessage.data.isNotEmpty()) {
// Applozic message handling you can copy paste this in your file
if (Applozic.isApplozicNotification(this, remoteMessage.data)) {
Log.i(TAG, "Applozic notification processed")
return
}
}
// Your own app notifications handling
}
Setup FCM push notification code in your android app
If you already have Firebase account and code to retrieve firebase registration token for the client app instance, skip this step.
1) If you haven't already added firebase to your project, add Firebase to your Android project
2) Once you're done with above setup, For getting FCM server key from firebase click Settings icon --> Project settings


3) Once you clicked project settings select Cloud Messaging tab under Settings. Under Project Credentials, copy your Server Key which is highlighted blue in the below following image


4) Update FCM server key in applozic dashboard
Go to your Applozic Dashboard Push Notification section and put your GCM/FCM server key.
5) In case if you don't have the existing FCM related code follow this
a) Copy paste push notification class files from this
- Github java link Or Kotlin link into your app
b) Edit your app manifest, Add the following to your app's AndroidManifest
file :
<service android:name="<CLASS_PACKAGE>.FcmListenerService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
6) To Update token to applozic server, Setup the registerForPushNotification task
by adding the below lines of code in onSuccess()
callback method of Applozic.connectUser
[Refer here]:
if (MobiComUserPreference.getInstance(this).isRegistered()) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w("FCM", "getInstanceId failed:", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.i("FCM", "Found token :" + token);
Applozic.registerForPushNotification(context, token, new AlPushNotificationHandler() {
@Override
public void onSuccess(RegistrationResponse registrationResponse) {
}
@Override
public void onFailure(RegistrationResponse registrationResponse, Exception exception) {
}
});
}
});
}
FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w("FCM", "getInstanceId failed:", task.exception)
[email protected]
}
val token = task.result?.token
Log.i("FCM", "Found token :$token")
if (token == null) {
Log.i("FCM", "FCM token is null returning")
[email protected]
}
Applozic.registerForPushNotification(
context,
token,
object : AlPushNotificationHandler {
override fun onSuccess(registrationResponse: RegistrationResponse?) {
Log.i("FCM", "Token updated to applozic server")
}
override fun onFailure(
registrationResponse: RegistrationResponse?,
exception: java.lang.Exception?
) {
}
})
})
Have Question Or Suggestion?
You can drop us your query at [email protected]
Custom UI
The steps are same as mentioned above in Drop-in UI section.
One more extra step for handling the notification click
Notification click handling and passing data to your activity
- Applozic can handle the showing of applozic notification and it will pass the data to your activity once you click on the notification to open
Add this metadata this 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 from the intent
Intent intent = getIntent();
Channel channel = null;
Contact contact = null;
if(intent != null){
String messageJson = intent.getStringExtra(MobiComKitConstants.MESSAGE_JSON_INTENT);
if (!TextUtils.isEmpty(messageJson)) {
Message message = (Message) GsonUtils.getObjectFromJson(messageJson, Message.class);
if (message.getGroupId() != null) {
channel = ChannelService.getInstance(this).getChannelByChannelKey(message.getGroupId());
} else {
AppContactService appContactService = new AppContactService(this);
contact= appContactService.getContactById(message.getContactIds());
}
}
}
if(channel != null){
Log.i("Channel","Channel/group object :"+channel);
}else if(contact != null){
Log.i("contact","Contact/user object :"+contact);
}
val intent = intent
var channel: Channel? = null
var contact: Contact? = null
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
if (message.groupId != null) {
channel = ChannelService.getInstance(this).getChannelByChannelKey(message.groupId)
} else {
val appContactService = AppContactService(this)
contact = appContactService.getContactById(message.contactIds)
}
}
}
if (channel != null) {
Log.i("Channel", "Channel/group object :$channel")
} else if (contact != null) {
Log.i("contact", "Contact/user object :$contact")
}
Sync messages
Whenever the connectivity changes like on internet connection connected and on your app activity open call the below method to sync messages from the server.
SyncCallService.getInstance(this).syncMessages(null);
SyncCallService.getInstance(this).syncMessages(null)
Updated over 1 year ago