Suggestions

close search

Android Push notification

When a Stringee call is made, Stringee Server pushes a message to the client who receives the call. If the client has already connected to Stringee Server, the incoming call can be received via StringeeConnectionListener.onIncomingCall(StringeeCall stringeeCall) and you can ignore the pushed message. Otherwise (the client disconnects from Stringee Server or the app is killed) the client only receives a message pushed from Stringee Server.

Push flow

Then the client must connect to Stringee Server to receive the incoming call. This tutorial will walk you through the steps of integrating push notification into your app to receive Stringee push message.

1. Create API project

We're using Firebase Cloud Messaging (FCM) for push notification. So you must create a Firebase project in the Firebase console for Cloud Messaging projects. To create a FireBase project, go to https://console.firebase.google.com/

Create a FireBase project

After creating a new FireBase project, add Firebase to your Android app

Add FireBase to Android app

After adding app, download a google-services.json file.

Download google-services.json file

2. Set up your project for push notification

Go to Firebase project console, choose Project Settings ==> Cloud Messaging, you'll get the Server key. Server key

Go to Stringee Dashboard, choose Push Notificaton -> Choose your project -> Choose your app if existing or create new app -> Enter the Server key, project package name.

Stringee app

3. Setup Firebase and add FCM SDK to your app

Copy google-services.json (From step 1) file into the app/ directory of your Android Studio project.

Add the dependency to your project-level build.gradle:

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.1.1' // google-services plugin
    }
}
allprojects {
    // ...
    repositories {
        // ...
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

Add the plugin at the bottom line of your app-level build.gradle:

apply plugin: 'com.google.gms.google-services'

In Android Studio, add the FCM dependency to your app-level build.gradle file:

dependencies {
     compile 'com.google.firebase:firebase-messaging:11.8.0'
}

4. Device registration token

Retrieve the current registration token

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. When you need to retrieve the current token, call

FirebaseInstanceId.getInstance().getToken()

This method returns null if the token has not yet been generated.

Register the registration token to Stringee Server

In order to receive push notification, you must register the token to Stringee Server by calling:

stringeeClient.registerPushToken(token, new StringeeClient.RegisterPushTokenListener() {
    @Override
    public void onPushTokenRegistered(int r, String desc) {
    }

    @Override
    public void onPushTokenUnRegistered(int r, String desc) {
    }
});

When the token is registered to Stringee Server, the onPushTokenRegistered is called:

Make sure that the token is registered to Stringee Server successfully. Otherwise, you can not receive push notification.

Monitor token generation

To handle the creation, rotation, and updating of registration tokens, use a service that extends FirebaseInstanceIdService. To use FirebaseInstanceIdService, you need to add the following in your app manifest:

<service
    android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

The onTokenRefresh callback of the FirebaseMessagingService service fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh, and register it to Stringee Server again:

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // Register the token to Stringee Server
    stringeeClient.registerPushToken(refreshedToken, new StringeeClient.RegisterPushTokenListener() {
        @Override
        public void onPushTokenRegistered(int r, String desc) {
        }

        @Override
        public void onPushTokenUnRegistered(int r, String desc) {
        }
    });
}

5. Receive messages

To receive messages, use a service that extends FirebaseMessagingService.Your service should override the onMessageReceived and onDeletedMessages callbacks. To use FirebaseMessagingService, you need to add the following in your app manifest:

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
Override onMessageReceived

By overriding the method FirebaseMessagingService.onMessageReceived, you can perform actions based on the received RemoteMessage object. You must check whether the client connects to Stringee Server. If connected, do nothing. Otherwise, the client need connect to Stringee Server and the incoming call is received via StringeeConnectionListener.onIncomingCall(StringeeCall stringeeCall).

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        String pushFromStringee = remoteMessage.getData().get("stringeePushNotification");
        if (pushFromStringee != null) { // Receive push notification from Stringee Server
            // Connect to Stringee Server here
        }
    }    
}

6. Unregister the registration token.

When you no longer want to receive push notification from Stringee server, call the unregisterPushToken method of StringeeClient:

stringeeClient.unregisterPushToken(token, new StringeeClient.RegisterPushTokenListener() {
    @Override
    public void onPushTokenRegistered(boolean success, String desc) {

    }

    @Override
    public void onPushTokenUnRegistered(boolean success, String desc) {
    }
});