Enable GCM/FCM

GCM/FCM adds robustness to starting trips and shifts from the server-side APIs and significantly reducing the time to first location. This guide will help you enable GCM integration for your app.

Setup GCM/FCM client for your app

[info] GCM/FCM already setup

In case GCM/FCM integration has already been set-up in your app, you can skip this and head to the next section.

These steps will setup a Firebase project for your app, and add the google-services configuration file to your codebase. For detailed info you can refer to official documentation.

Create an API project

New Cloud Messaging projects must create a Firebase project in the Firebase console. In this process, you will generate a configuration file and credentials for your project.

  1. Create a new project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  2. Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
  3. When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
  4. At the end, you'll download a google-services.json file. You can download this file again at any time.
  5. If you haven't done so already, copy this into your project's module folder, typically app/.

Add the configuration file to your project

The Google Services plugin for Gradle parses configuration information from the google-services.json file. Add the plugin to your project by updating your top-level build.gradle and your app-level build.gradle files as follows:

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

    buildscript {
     ...
       dependencies {
          ...
           classpath 'com.google.gms:google-services:3.0.0'
       }
    }
    
  2. Add the plugin to bottom your app-level build.gradle:

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

Configure GCM/FCM Server key on HyperTrack

Head over to the account settings on the HyperTrack dashboard to setup your keys. These keys are only used to send triggers to start trips or shifts to the SDK.

[info] Locate your key

Note the Server key is available in your new project under Project settings > Cloud Messaging. You'll need to configure this key to enable GCM.

Note that you need to configure either one of GCM or FCM Server Key in the dashboard settings depending on the whether you have integrated GCM or FCM in your app. For detailed info you can refer to official documentation.

GCM Key Settings

Pass notification to SDK

[warning] Optional step

This step is required in case either GcmListenerService or FirebaseMessagingService is already registered in your app, which means you have registered a service to receive GCM/FCM Notifications in your AndroidManifest.xml file.

For GCM Integration

This step is required in case GcmListenerService is already registered in your app, which means you have registered a service to receive GCM Notifications in your AndroidManifest.xml file.

Add the following code snippet in the onMessageReceived method of your GcmListenerService service to pass the HyperTrack notifications to the SDK.

@Override
public void onMessageReceived(String from, Bundle data) {

  String htSDKNotification = data.getString(HTGcmListenerService.HT_SDK_NOTIFICATION_KEY);
  if (htSDKNotification != null && htSDKNotification.equalsIgnoreCase("true")) {
    HTGcmListenerService.triggerHypertrackNotification(getApplicationContext(), data);
    return;
  }

  ...
}

For FCM Integration

Add the following code snippet in the onMessageReceived method of your FirebaseMessagingService service to pass the HyperTrack notifications to the SDK.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

  if (remoteMessage != null && remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
    String htSDKNotification = remoteMessage.getData().get(HTFirebaseMessagingService.HT_SDK_NOTIFICATION_KEY);
    if (htSDKNotification != null && htSDKNotification.equalsIgnoreCase("true")) {
      HTFirebaseMessagingService.triggerHypertrackNotification(getApplicationContext(), remoteMessage.getData());
      return;
    }
  }

  ...
}

results matching ""

    No results matching ""