How to use the Bubbl Plugin in your app

📘

Objective

Implement the Android Bubbl Plugin into an Android app written in Java.

📘

Audience

Android App Developer

Add an Application Class to your App

For the most reliable operation, the Bubbl Plugin should be initialised from within your Application.onCreate() method.

The Bubbl Plugin uses google location services. We need to add a check if a user has disabled location permission for your app in the phone's settings, then you have to request that permission. Below you'll find a sample piece of code on how to do it. In addition to this we override the runtime permission call back as well before we initialise the Bubbl Plugin with your API_KEY.

By setting checkGps to true the plugin will handle all location permission and device location on off checking. Setting checkGps to false requires the app developer to handle the location permissions and the device location on off checking.

You will find your API_KEY on the Bubbl Dashboard Company page.

import androidx.annotation.Nullable;
...
import tech.bubbl.sdk.BubblSdk;
import tech.bubbl.sdk.BubblSdkManager;

import android.content.Intent;
...
public class MainActivity extends AppCompatActivity {
    ...
    public void onCreate() {
        ...
        BubblSdk.initialize(this, "https://mobilegateway.bubbl.tech/api/", "your-bubbl-api-key", true, true);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                            String permissions[],
                                            int[]  grantResults) {
        BubblSdk.requestPermissionResult(MainActivity.this,
                                         requestCode,
                                         permissions,
                                         grantResults
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        BubblSdkManager.getInstance(getApplicationContext()).setOnActivityResult(requestCode, resultCode, data);
    }
}

In order to receive notification from API 26 and above we need to provide a notification channel.

<resources>
...
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel_id</string>
    <string name="default_notification_channel_name" translatable="true">fcm_default_channel_name</string>
...
</resources>
...
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
...
import tech.bubbl.sdk.BubblSdk;
...
public class MainActivity extends AppCompatActivity {
    ...
    public void onCreate() {
        ...
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId  = getString(R.string.default_notification_channel_id);
            String channelName = getString(R.string.default_notification_channel_name);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(
                new NotificationChannel(channelId,
                                        channelName,
                                        NotificationManager.IMPORTANCE_HIGH)
            );
        }
        ...
    }
}

In order to receive location updates at anytime, including while your app is running in the background, we register our broadcast receiver on the Activity onStart() and onStop() override methods

If the checkGps set to true then it needs to be added to any of your exit cycle, i.e. onPause(), onStop() or onDestroy().

removeCheckLocationOnOff() is called from Bubbl Plugin so it can initialise the LocationSettingsRequest Builder.

This also needs to be overridden in the onActivityResult() method in your MainActivity with the setOnActivityResult call from the BubblSdkManager.

import tech.bubbl.sdk.BubblSdk;
import tech.bubbl.sdk.BubblSdkManager;
...
public class MainActivity extends AppCompatActivity {
    ...
    @Override
    protected void onStart() {
        super.onStart();
        BubblSdkManager.getInstance(getApplicationContext()).registerBroadcastReceiver(getApplicationContext());
        BubblSdkManager.getInstance(getApplicationContext()).registerBatteryInfoReceiver(getApplicationContext());
    }

    @Override
    protected void onStop() {
        super.onStop();
        BubblSdk.removeCheckLocationOnOff(getApplicationContext());
        BubblSdkManager.getInstance(getApplicationContext()).registerBroadcastReceiver(getApplicationContext());
        BubblSdkManager.getInstance(getApplicationContext()).registerBatteryInfoReceiver(getApplicationContext());
    }
    ...
}

To enable the user to receive Bubbl Plugin notifications when the app is running in the background add the following:

import tech.bubbl.sdk.BubblSdk;
...
public class MainActivity extends AppCompatActivity {
    ...
    @Override
    public void onBackPressed() {
        BubblSdk.onBackPressed(MainActivity.this);
    }
    ...
}