Android Native Bridge implementation
Objective
To provide a React Native App Developer with details on how to implement the React Native Bridge to enable communication between React Native and the Android Bubbl Plugin.
Audience
React Native App Developer
Add the following file, remembering to change the com/yourpackage
to the package of your app.
package com.yourpackage;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomBubblSdkPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new BubblSdkModule(reactContext));
return modules;
}
}
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import tech.bubbl.sdk.BubblSdk;
import tech.bubbl.sdk.BubblSdkManager;
...
public class MainActivity extends ReactActivity {
...
@SuppressLint("WrongConstant")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = this;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create a channel to show notifications.
String notification_channel_id = getString(R.string.default_notification_channel_id);
String Notification_channel_name = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(
new NotificationChannel(
notification_channel_id,
Notification_channel_name,
NotificationManager.IMPORTANCE_HIGH
)
);
}
}
...
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[],
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
BubblSdk.requestPermissionResult(
getActivity(),
requestCode,
permissions,
grantResults
);
}
...
}
List<ReactPackage> packages = new PackageList(this).getPackages();
...
packages.add(new CustomBubblSdkPackage());
...
return packages;
}
Note
Do NOT change the file name from BubblSdkModule.java.
package com.yourpackage;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import tech.bubbl.sdk.BubblSdk;
import tech.bubbl.sdk.BubblSdkManager;
public class BubblSdkModule extends ReactContextBaseJavaModule {
private static ReactApplicationContext reactContext;
BubblSdkModule(ReactApplicationContext context) {
super(context);
reactContext = context;
}
@Override
public String getName() {
return "BubblSdk";
}
@ReactMethod
public void initialize(String apiUrl, String apiKey) {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdk.initialize(MainActivity.getActivity(), apiUrl, apiKey, true, true);
}
});
}
@ReactMethod
public void reinitialize(String apiUrl, String apiKey) {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdk.reInitialize(MainActivity.getActivity(), apiUrl, apiKey, true, true);
}
});
}
@ReactMethod
public void saveBubblId(String bubblId) {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdk.setBubblId(getReactApplicationContext(), bubblId);
}
});
}
// Bubbl provides Information screens (Activities), that you may use,
// both for development purposes and as a part of your app. You can use
// these additions to your Native Bridge for triggering corresponding screens,
// they should **NOT** be used for production.
// Add, as required, the Java method to your `BubblSdkModule` file and the React Native code to your `App.js` file.
@ReactMethod
public void payloadActivity() {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdkManager.getInstance(getReactApplicationContext()).openPayloadActivity(getReactApplicationContext());
}
});
}
@ReactMethod
public void dataActivity() {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdkManager.getInstance(getReactApplicationContext()).openDataActivity(getReactApplicationContext());
}
});
}
@ReactMethod
public void logsActivity() {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdkManager.getInstance(getReactApplicationContext()).openLogsActivity(getReactApplicationContext());
}
});
}
@ReactMethod
public void mapActivity() {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdkManager.getInstance(getReactApplicationContext()).openMapActivity(getReactApplicationContext());
}
});
}
@ReactMethod()
public void getSegmentationTags(Promise promise) {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String segmentationTags = BubblSdk.getSegmentationTags(MainActivity.getActivity());
promise.resolve(segmentationTags);
} catch (Exception e) {
promise.reject("Create Event Error", e);
}
}
});
}
@ReactMethod
public void setSegmentationTags(String tags) {
MainActivity.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BubblSdk.setSegmentationTags(MainActivity.getActivity(), tags);
}
});
}
}
public class MainActivity extends ReactActivity {
private static Activity currentActivity;
...
public static Activity getActivity() {
Activity activity = new Activity();
activity = currentActivity;
return activity;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[],
int[] grantResults) {
BubblSdk.requestPermissionResult(
getActivity(),
requestCode,
permissions,
grantResults
);
}
@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());
}
/**
* Back button listener.
* Will close the application if the back button pressed twice.
*/
@Override
public void onBackPressed()
{
BubblSdk.onBackPressed(MainActivity.this);
}
...
}
Updated over 1 year ago