Real-time support

GCM/FCM allows the SDK to be controlled by the server for better battery optimization along with real-time support in the SDK. This also adds robustness in the SDK by enabling the sdk to recover from app crashes. Use the relevant HyperTrack listener services in your code to route HyperTrack notifications to the SDK.

[info] Realtime support Onboarding

We recommend you try our new interactive step-by-step onboarding to enable realtime support. Refer here for more details or in case of any issues.

Android Realtime support Onboarding

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:

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

Locate your GCM/FCM key

Head over to your Firebase Console and locate your Server key under Project settings > Cloud Messaging.

GCM Keys

Configure GCM/FCM key on HyperTrack

Head over to the account settings on the HyperTrack dashboard to setup your keys.

Note that you need to configure either one of GCM or FCM Server Key on HyperTrack, depending on the whether you have integrated GCM or FCM in your app. For detailed information, refer to official Google documentation.

GCM Key Settings

Pass notification to SDK

For FCM Integration

To enable FCM integration, you need to change the base class for your implementation of FirebaseMessagingService to HyperTrackFirebaseMessagingService.

[info] Fresh FCM Integration

Add a new Service which extends HyperTrackFirebaseMessagingService to your application's manifest file with the tag as highlighted in the code snippet.

// Extend HyperTrackFirebaseMessagingService instead of FirebaseMessagingService
public class MyFirebaseMessagingService extends HyperTrackFirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        /**
         * Call super.onMessageReceived() method
         * SDK uses this method to handle HyperTrack notifications
         * Refer to the https://dashboard.hypertrack.com/onboarding/fcm-android 
         * for more info.
         */
        super.onMessageReceived(remoteMessage);

        ...
    }
}
<!-- For fresh FCM integration, register the new file in your app's AndroidManifest.xml file -->
<service android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

For GCM Integration

To enable GCM integration, you need to change the base class for your implementation of GcmListenerService to HyperTrackGcmListenerService.

[info] Fresh GCM Integration

Add a new Service which extends HyperTrackGcmListenerService to your application's manifest file with the tag as highlighted in the code snippet.

// Extend HyperTrackGcmListenerService instead of GcmListenerService
public class MyGcmListenerService extends HyperTrackGcmListenerService {
    @Override
    public void onMessageReceived(String from, Bundle data) {
        /**
         * Call super.onMessageReceived() method
         * SDK uses this method to handle HyperTrack notifications
         * Refer to the https://dashboard.hypertrack.com/onboarding/fcm-android 
         * for more info.
         */
        super.onMessageReceived(data);

        ...
    }
}
<!-- For fresh GCM integration, register the new file in your app's AndroidManifest.xml file -->
<service android:name=".MyGcmListenerService">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

Handle your notifications

[warning] Handle your notifications

To handle your notifications, you need to ignore HyperTrack notifications. In case you dont ignore them, your app might end up in a crash.

For FCM notifications

public class MyFirebaseMessagingService extends HyperTrackFirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        ...

        if (remoteMessage.getData() != null) {
            String sdkNotification = remoteMessage.getData().get(HT_SDK_NOTIFICATION_KEY);
            if (sdkNotification != null && sdkNotification.equalsIgnoreCase("true")) {
                /**
                 * HyperTrack notifications are received here
                 * Dont handle these notifications. This might end up in a crash
                 */
                return;
            }
        }

        // Handle your notifications here.
    }
}

For GCM notifications

public class MyGcmListenerService extends HyperTrackGcmListenerService {
    @Override
    public void onMessageReceived(String from, Bundle data) {
        ...

        if (data != null) {
            String sdkNotification = data.getString(HT_SDK_NOTIFICATION_KEY);
            if (sdkNotification != null && sdkNotification.equalsIgnoreCase("true")) {
                /**
                 * HyperTrack notifications are received here
                 * Dont handle these notifications. This might end up in a crash
                 */
                return;
            }
        }

        // Handle your notifications here.
    }
}

Test your GCM/FCM integration

Sends a Test UI notification to your device from HyperTrack Server.

curl -H "Authorization: token YOUR_SK_TOKEN" \
     -H "Content-Type: application/json" \
     -X POST \
     https://api.hypertrack.com/api/v1/users/<user-id>/test_notification/

results matching ""

    No results matching ""