Android Features – Real-time Data Sync

Introduction

Certain use cases demand that Geo-fences, Beacons, and GEOLINE™, as well as the Actions and Conditions associated with them on the back-end, are synced to the mobile devices immediately. Bluedot Point SDK for Android can be optionally configured to receive push notifications to enable Real-time Data Sync. The present implementation supports integration through Google Firebase cloud messaging; future versions of the SDK will provide support for any other push infrastructures, giving you the flexibility to connect your existing push integrations. This documentation will guide you through the integration processes and steps to activate the Real-time Data Sync.

Integrate Firebase SDK into your project

To integrate Firebase, below are the prerequisites that must be met:

To add Firebase to your app, you will need to create a Firebase project and a Firebase configuration file. This can be achieved by following steps:

1. Create a new Firebase project in the Firebase console. If you have an existing Google project associated with your mobile app, you can import it by clicking Import Google Project. To create a new project click on Create New Project.

2. A new dialog will appear. Enter your project’s name and select your country/region and click Create Project.

3. You will be redirected to the project dashboard. Select Add Firebase to your Android app.

4. A new dialog will appear asking for a package name and an optional SHA-1 hash of your debug signing certificate. The package name can be retrieved from either the AndroidManifest.xml or the build.gradle file. Select ADD APP to add your app to Firebase project.

//Package name is called applicationId in build.gradle
 
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
 
    defaultConfig {
        applicationId "com.test.myfirstproject"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
...
}
 
//In AndroidManifest.xml package name lies under manifest tag
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.test.myfirstproject">
 
....
 
</manifest>

To retrieve the SHA-1 hash from the KeyStore, open Terminal on Mac/Linux or Command Prompt on Windows, and paste the relevant line below. If the prompt asks for a password, enter default password android.

// MAC/LINUX
 
keytool -exportcert -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore
 
// WINDOWS
 
keytool -exportcert -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore
 
// Copy the SHA1 from certificate details, below is an example of how SHA1 looks like.
 
Certificate fingerprint: SHA1: DA:39:A3:EE:5E:6B:4B:0D:32:55:BF:EF:95:60:18:90:AF:D8:07:09

5. A google-services.json file will be downloaded and saved to your browser’s default download directory. In Android Studio switch to Project view and move the downloaded google-services.json file into the app folder. Once done, press CONTINUE.

6. Modify your app’s project-level build.gradle file and add the following line:

buildscript {
  dependencies {
    // Add this line
    classpath 'com.google.gms:google-services:3.2.1'
    ...
  }
}

7. Modify your app’s app-level build.gradle file and add the lines below:

android {
  // ...
}
 
dependencies {
  // Add these lines in dependencies, to check for latest available versions, please visit https://firebase.google.com/docs/android/setup
  compile 'com.google.firebase:firebase-core:15.0.0'
  compile 'com.google.firebase:firebase-messaging:15.0.0'
}
 
...
// Add this line at bottom of the file
apply plugin: 'com.google.gms.google-services'

8. Sync the project gradle file by pressing Sync project with Gradle files as shown below:

9. Create a new Service with your desired name that extends FirebaseMessagingService and override its onMessageReceived method.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        ...
    }
}

10. Edit the AndroidManifest.xml file and the service declaration inside the application tag.

<application>
...
 
<!--Add these lines-->
<service
 android:name=".MyFirebaseMessagingService">
 <intent-filter>
 <action android:name="com.google.firebase.MESSAGING_EVENT"/>
 </intent-filter>
</service>
 
...
</application>

11. By default the Firebase SDK integration enables analytics collection for your app. To temporarily or permanently disable analytics on your app, there are two options available, shown below:

// Method 1: Add a meta-data tag in AndroidManifext.xml file inside application tag
 
<application
 ... >
 
<!-- Add this line to disable Firebase analytics -->
<meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
 
...
</application>
// Method 2: Add this line in the class code to disable analytics collection
FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(false);

Bluedot Push Interface

To enable Push notifications from our Bluedot app, we need to subscribe to the topic using our Bluedot’s application API key.  This is achieved using the code below. It is recommended to add the line to the onBluedotPointServicesStartedSuccess() callback.

@Override
public void onBlueDotPointServiceStartedSuccess() {
    //Add this line
    FirebaseMessaging.getInstance().subscribeToTopic("/topics/" + BLUEDOT_API_KEY);
    ...
}

The next step is to provide the received push notification’s data object to Bluedot’s notifyPushUpdate method, to process the push and trigger rule set refresh. This method is to be placed inside the onMessageReceived callback of your service that extends FirebaseMessagingService, demonstrated below.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        //Add these lines
        ServiceManager serviceManager = ServiceManager.getInstance(this);
        serviceManager.notifyPushUpdate(remoteMessage.getData());
        ...
    }
}
image

Disclaimer: Bluedot Point SDK will only utilize the data of push notifications sent containing a unique Bluedot identifier, and will ignore all other push notifications.

The last step is to provide the Firebase Server key of your project to the Bluedot Point Access dashboard. The Firebase server key can be retrieved by opening Project Settings from your Firebase dashboard and opening the Cloud Messaging tab. From there you can copy the Server key.

On your Bluedot Point Access dashboard, select App and Beacon Managment, and from the list of apps, select More Details for the app for which you want to enable push notifications. Paste the Firebase server key into the Firebase API Key field and Save it.

Created by Bluedot DevOps on January 16, 2018