Tutorials

Applozic Android SDK Tutorial 4 – Push Notifications

Integrate Applozic Android SDK to get started with your own chat application!
Google+ Pinterest LinkedIn Tumblr

This tutorial is Part 4 of a multi-part series covering Applozic Android SDK integration into an Android app.

Our goal is to learn the following steps in this tutorial:

  1. Create FCM key for your app in Firebase
  2. Enable FCM notifications from Applozic dashboard
  3. Integrate Firebase handler in your app
  4. Process the received notifications

You can download the sample Android chat app from Applozic GitHub.

Note: If you already have FCM set up for your app, move directly to Step 2. 

Firebase and FCM key

Firebase is a mobile application development platform from Google with powerful features for developing, handling, and enhancing highly functional and versatile web, Android, and iOS platform applications.

Please note that FCM (Firebase Cloud Messaging) was previously called GCM (Google Cloud Messaging).

To learn more about Firebase’s features and use cases, check out this article.

Generate FCM key in Firebase

Login to the Firebase Developer Console and click on “Create a project“.
Firebase main screen
Firebase Main Screen
In your project dashboard, click on the “Settings” icon > “Project settings”.
Project settings
Project settings
Go to the “Cloud Messaging” tab.
Cloud Messaging tabs
Cloud Messaging tabs
The “Server key” and the “Sender ID” are your private credentials.
Leave this tab open and proceed to the next step.
Server key location
Server key location

Set up FCM Notifications

The Server key is an API key that is a unique string used to route requests to your Firebase project when interacting with Firebase and Google services.

Your FCM credentials are unique to the project you have created, so keep them secure and do not share them with anyone.

Update FCM Server key in Applozic Dashboard.

Go to your Applozic Dashboard. Click on “Settings”.
Settings in Applozic Dashboard.
Settings in Applozic Dashboard
Click on “Chat” > “Notifications”.
Notification tab under Chat
Notification tab under Chat
Click “Add Credentials” under FCM and paste the “Server Key” from Firebase credentials.
Add FCM Credentials
Add FCM Credentials
Voila! Your app is set up for use with FCM.

Integrate FCM in your application

If your app does not have Firebase handler code, copy the following code into a new class to receive FCM messages in your app:

package com.applozic.mobicomkit.sample.pushnotification;

import android.util.Log;

import com.applozic.mobicomkit.Applozic;
import com.applozic.mobicomkit.api.account.register.RegisterUserClientService;
import com.applozic.mobicomkit.api.account.register.RegistrationResponse;
import com.applozic.mobicomkit.api.account.user.MobiComUserPreference;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;


public class FcmListenerService extends FirebaseMessagingService {

    private static final String TAG = "ApplozicGcmListener";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.i(TAG, "Message data:" + remoteMessage.getData());

        if (remoteMessage.getData().size() > 0) {
            if (Applozic.isApplozicNotification(this,remoteMessage.getData())) {
                Log.i(TAG, "Applozic notification processed");
                return;
            }
        }
    }

    @Override
    public void onNewToken(String registrationId) {
        super.onNewToken(registrationId);

        Log.i(TAG, "Found Registration Id:" + registrationId);
        Applozic.getInstance(this).setDeviceRegistrationId(registrationId);
        if (MobiComUserPreference.getInstance(this).isRegistered()) {
            try {
                RegistrationResponse registrationResponse = new RegisterUserClientService(this).updatePushNotificationId(registrationId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

If your app already has a Firebase handler, add the following code to get FCM Registration token and pass it to Applozic:

@Override
    public void onNewToken(String registrationId) {
        super.onNewToken(registrationId);

        Log.i(TAG, "Found Registration Id:" + registrationId);
            Applozic.getInstance(this).setDeviceRegistrationId(registrationId);
        if (MobiComUserPreference.getInstance(this).isRegistered()) {
            try {
                RegistrationResponse registrationResponse = new RegisterUserClientService(this).updatePushNotificationId(registrationId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

In your AndroidManifest.xml file, add the following code:

<service android:name="<CLASS_PACKAGE>.FcmListenerService"
         android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

Process the Received Notifications

In your UserLoginTask(refer to Tutorial 2 or check the documentation here), add the below lines of code in onSuccess() callback method of Applozic.connectUser.

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) {
                                }
                            });
                        }
                    });
        }

End of Tutorial 2

In this Applozic Android SDK tutorial about push notifications, we have learnt how to:

  • Create FCM key in Firebase
  • Enable FCM notifications from Applozic dashboard
  • Integrate Firebase handler in your app
  • Process the received notifications

Please leave your feedback in the comments below!

Author

Anurag is a Technical Writer and technology enthusiast who enjoys science fiction and PC gaming.