Firebase Cloud Messaging

The SDK supports receiving a subset of events via FCM. The events currently supported over FCM are:

  • message:create
  • message:update

Events received directly from the server and events received via FCM will be monitored by the SDK to ensure you will only receive each event once.

Generate a Firebase Key

You will first need to generate a Firebase key to allow the server to send Firebase Messages to your application:

  • Navigate to https://firebase.google.com.
  • Select Create New Project.
  • Give the project a name and click Create Project.
  • Select the icon labeled Add Firebase to your Android App.
  • Enter your app's package name and click Add App.
  • This will download a file called google-services.json to your machine. We will use this file in a moment.
  • Now select Project Settings from the menu on the left by clicking the gear icon and then select the Cloud Messaging tab.
  • Copy the full Server key field. We will use this in the next step.

Add the Firebase Server Key to your VoiceLayer app

  • Log into your app's dashboard at https://portal.voicelayer.io/apps
  • Select the My Apps tab.
  • Select Manage your App on the app you would like to configure.
  • Select Push Notifications in the menu on the left.
  • Click the Configure FCM button and enter the Server key from the previous section.

Add the google-services.json file to your app

  • Add the google-services.json file to your app by copying it into your app's app directory.
    • This will commonly be located at project-folder/app/.
    • If your project does not have an app directory look for the directory that contains the src directory.

Include the Firebase Messaging dependency

  • Add classpath 'com.google.gms:google-services:3.0.0' to the dependencies section of your app's top level build.gradle file.
  • Add compile 'com.google.firebase:firebase-messaging:10.0.1' to the dependencies section of your app's app level build.gradle file.
  • Add apply plugin: 'com.google.gms.google-services' at the bottom of your app's app level build.gradle file.

Implement the VoiceLayerNotificationService

First create a new class, named whatever you like, and have it extend the VoiceLayerNotificationService:

public class FirebaseMessageService extends VoiceLayerNotificationService {

    public FirebaseMessageService() {}

    // Called if a non-VoiceLayer message is received
    @Override
    public void onFirebaseMessageReceived(RemoteMessage message) {}

}

Then register the service in your application manifest:

<service android:name=".Services.FirebaseMessageService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

This service will automatically handle incoming FCM messages related to VoiceLayer. Any messages that are not associated with VoiceLayer will be passed on to your service via the onFirebaseMessageReceived method.

Register the Firebase device identifier with the server

The server needs to be told what your device's Firebase identifier is in order to target messages at your device. In order to do this you will need to retrieve the token and then send it to the server. To retrieve the token extend the FirebaseInstanceIdService and register it in your manifest:

<service android:name=".Services.FirebaseIdentifierService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
public class FirebaseIdentifierService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        // Retrieve the token
        String token = FirebaseInstanceId.getInstance().getToken();

    }

}

You can cache this token however you like. You can then associate that identifier with a user via the following call once a user logs in or registers:

VoiceLayerNotificationToken serverToken = new VoiceLayerNotificationToken(token);
VoiceLayerClient.getInstance().registerNotificationToken(serverToken, null);

Note that this token is automatically disassociated with the user upon calling logout or upon registering a different token for the same user. However if you need to unregister the identifier with the user you can manually call:

VoiceLayerClient.getInstance().unregisterNotificationToken(null);