Enable GCM/FCM

GCM/FCM allows the SDK to be controlled by the server for better battery optimization along with realtime-ness 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.

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

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.

// Add a new class which extends `HyperTrackFirebaseMessagingService`
public class AppFCMListenerService extends HyperTrackFirebaseMessagingService {
    // Extend HyperTrackFirebaseMessagingService instead of FirebaseMessagingService
    ...
}
<!-- For fresh FCM integration, register the new file in your app's `AndroidManifest.xml` file -->
<service android:name=".AppFCMListenerService">
    <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.

// Add a new class which extends `HyperTrackGcmListenerService`
public class AppGCMListenerService extends HyperTrackGcmListenerService {
    // Extend HyperTrackGcmListenerService instead of GcmListenerService
    ...
}
<!-- For fresh GCM integration, register the new file in your app's `AndroidManifest.xml` file -->
<service android:name=".AppGCMListenerService">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

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/d0ae4912-2074-45ef-a7c0-76be58639ea9/send_notification/

results matching ""

    No results matching ""