Android Push Notification support

Oracle CX Core SDK enables tracking of application opening by a click on push notification message automatically or manually based on the configurations.

Tracking push notification events automatically

SDK provides several ways for configuring the app to track the push notification events automatically:

  1. Creating a notification by using ORANotification class.
  2. Passing the required key value pair to intent extra info using Android push notification.
  3. Creating a notification through Firebase Cloud Messaging setting required data pair.

General requirements

To enable automatic tracking of notification events you should:

  • Set PUSH_AUTO_ENABLED the config value to "true" as below.

Add below line in oracle.json

"ora_dc_push_auto_enabled" : "true"

ORACoreDataContainer container = new ORACoreDataContainer(this);
container.putValue(ORABaseConfigSettings.PUSH_AUTO_ENABLED, "true");
  • Have "ora_notification" key as an intent extra value for push notification activity.

Creating a notification by using ORANotification class.

Example using ORANotification

Build your notification event using ORANotification class, which will add the required key to event data automatically.

final ORANotification builder = new ORANotification(this, "ChanelId")
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Notification title")
.setContentText("This is a test notification")
final Intent resultIntent = new Intent(context, NotificationActivity.class);
builder.setORAContentIntent(0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Passing the required key value pair to intent extra info using Android push notification.

Example using Android Notification

Build your notification event using android notification compat builder and pass the required "ora_notification" key to intent extra params.

final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, "ChanelId")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
//without below line notification can not be tracked
resultIntent.putExtra("ora_notification", "yourValue");
...
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
...
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

Or you can create your own chanel id for the devices with target API 27 (Android 8 Oreo) or above.

Creating a notification through Firebase Cloud Messaging setting required data pair.

Example using Firebase Messaging Service (FCM)

FCM example for application in foreground

When app is in foreground the FirebaseMessagingService's onMessageReceived method is called thus you can pass the required key value pair to your intent manually in the method.

resultIntent.putExtra("ora_notification", "yourValue");

And when automatics is enabled the SDK will be able to track your notification and send event when your notification is clicked.

FCM example for application in background or killed

When your app is in background the FirebaseMessagingService's onMessageReceived isn't called and you need to pass specific key value pair via request body 'data' field or using Firebase console "Additional Parameters" fields.

POST request example
curl -X POST -H "Authorization: Bearer yourFirebaseProjectServerKey...HnS_uNreA" -H "Content-Type: application/json" -d '{
"notification": {
"title": "FCM Message",
"body": "This is a Firebase Cloud Messaging Topic Message!",
},
"data": {
"ora_notification": "your_value"
},
"to" : "device_id_token" // or "/topics/yourTopicName"}' "https://fcm.googleapis.com/v1/projects/your_project_id/messages:send HTTP/1.1"

Firebase console configuration example

There are three ways to send notification from Firebase (console and by HTTP request):

  1. application package name - pushes notification to all apps with the package
  2. specific application token in device - pushes notification only to specific device
  3. specific topic - pushes notification to the devices which subscribes that topic
Segment config example
Topic config example
Single device config example

For each case you need to send the key value pair specified by Oracle CX Core SDK:

Advanced options setup

Tracking push notification events manually

The triggerNotificationEvent method can be used to manually track whether the application was opened by clicking on a notification event. The call can be put in onActivityStarted method where users can get the required info from intent extras and send in the event using customData.

public void onActivityStarted() {
Map<String, String> customData = new HashMap<String, String>();
.....
// some code to check that it's called by notification
....
// trigger the notification event.
ORADataCollector.getInstance().triggerNotificationEvent("Notification Message", "My Activity", customData);
super.onActivityStarted();
}