push advanced - optimove-tech/Optimove-SDK-Android GitHub Wiki

Overriding PushBroadcastReceiver

Default Push Behavior

By default, the Optimove PushBroadcastReceiver can show a notification on the notification area of the device, or as a heads-up notification when a content push is received with the appropriate setting selected.

Tapping this notification will open the main launcher activity of your application and track the push open conversion for you.

Your main activity will receive the push contents in its options bundle under the PushMessage.EXTRAS_KEY.

Customizing Push Behavior

To customize the behavior of the SDK when a push is received or its notification is tapped, we suggest subclassing the PushBroadcastReceiver and overriding its base methods depending on what you want to customize.

Example extension class:

package com.example;

import com.optimove.android.optimobile.PushBroadcastReceiver;

public class MyPushReceiver extends PushBroadcastReceiver {

}

Make sure to change the AndroidManifest.xml receiver:

<receiver android:name="com.example.MyPushReceiver" android:exported="false">
    <intent-filter>
        <action android:name="com.optimove.push.RECEIVED" />
        <action android:name="com.optimove.push.OPENED" />
        <action android:name="com.optimove.push.DISMISSED" />
        <action android:name="com.optimove.push.BUTTON_CLICKED" />
    </intent-filter>
</receiver>

You can override the following methods:

  • getPushOpenActivityIntent -- to change the activity launched when notification tapped
  • getNotificationBuilder -- to customize notification (*)
  • onBackgroundPush -- to perform work when a background data push is received
  • onPushReceived -- overriding all behaviours (* not adviceable)

Note: (*) overriding these methods may interfer with the correct functioning of the SDK, for example correct tracking of open events. Please, refer to the relevant section for details

Changing the Launched Activity

To change which activity will be launched when the user taps a notification, you can override the PushBroadcastReceiver#getPushOpenActivityIntent(Context, PushMessage).

package com.example;

import android.content.Context;
import android.content.Intent;

import com.optimove.android.optimobile.PushBroadcastReceiver;
import com.optimove.android.optimobile.PushMessage;

public class MyPushReceiver extends PushBroadcastReceiver {

    @Override
    protected Intent getPushOpenActivityIntent(Context context, PushMessage pushMessage) {
        // TODO implement your own logic here
        return super.getPushOpenActivityIntent(context, pushMessage);
    }
}

The PushMessage model will not be added to the Intent by default, it is up to you to add it as an extra if desired:

Intent launchIntent = new Intent(context, MyActivity.class);
launchIntent.putExtra(PushMessage.EXTRAS_KEY, pushMessage);

You can return null to track the push conversion and do nothing when the notification is tapped.

If the Intent returned does not describe an Activity, it will be ignored

Customizing the Notification

To customize the notification shown to the user for content pushes, you can override PushBroadcastReceiver#getNotificationBuilder(Context, PushMessage).

package com.example;

import android.app.Notification;
import android.content.Context;

import com.optimove.android.optimobile.PushBroadcastReceiver;
import com.optimove.android.optimobile.PushMessage;

public class MyPushReceiver extends PushBroadcastReceiver {
    @Override
    protected @Nullable Notification.Builder getNotificationBuilder(Context context, PushMessage pushMessage) {
        // get builder with Optimove intents set up
        Notification.Builder builder = super.getNotificationBuilder(context, pushMessage);
        // TODO customize the notification
        builder.setContentTitle("custom: " + pushMessage.getTitle());

        return builder;
    }
}

If you want to handle the open/dismissal with the broadcast receiver, and decide to use your new, custom Notification.Builder (not advisable), be sure to set up the intents of the notification correctly. Refer to PushBroadcastReceiver source. This will ensure that the notification conversion (opens, dismissed, delivered) is tracked in Optimove, that Optimove push action handler is fired and prevent possible Android 12 trampoline issues.

If you want to do something else, you can manually track push open conversion using Optimove.getInstance().pushTrackOpen(int) and track dismissed event using Optimove.getInstance().pushTrackDismissed(int). Additionally, you would have to add deep link extras for in-app message deep links to keep working.

Optimove.getInstance().pushTrackOpen(pushMessage.getId());
Optimove.getInstance().pushTrackDismissed(pushMessage.getId());
//call in the scope of MyPushReceiver
addDeepLinkExtras(pushMessage, launchIntent);

Launching a Service for Background Data Pushes

To perform work when a background data push is received, you can override PushBroadcastReceiver#onBackgroundPush.

package com.example;

import android.content.Context;
import android.content.Intent;

import com.optimove.android.optimobile.PushBroadcastReceiver;
import com.optimove.android.optimobile.PushMessage;

public class MyPushReceiver extends PushBroadcastReceiver {

    @Override
    protected void onBackgroundPush(Context context, PushMessage pushMessage) {
        WorkManager workManager = WorkManager.getInstance(context);
        workManager.enqueue(new OneTimeWorkRequest.Builder(MyWorker.class).build());
    }

}

Overriding All Behaviors

If you want to completely replace the logic for handling pushes, you can override PushBroadcastReceiver#onPushReceived(Context, PushMessage).

Bear in mind you will be responsible for all aspects of the push process such as showing a notification to the user, tracking an open conversion using Optimove.getInstance().pushTrackOpen(int) and dismissed events using Optimove.getInstance().pushTrackDismissed(int), or launching any activities or services.

In addition, you may need to implement behaviors for:

  • Delivery tracking: pushTrackDelivered(pushMessage)
  • In-App message sync: maybeTriggerInAppSync(pushMessage)
  • Clearing notifications for in-app messages requires presenting with the Optimove tag like so: notificationManager.notify(OPTIMOVE_NOTIFICATION_TAG, this.getNotificationId(pushMessage) , notification)

Using Your Own FirebaseMessagingService with Optimove

If you already consume FCM push notifications with your own FirebaseMessagingService but you want to also enjoy the benefits of the Optimove push service, you can use the SDK's helper methods in your own implementation. For example:

public class MyAppFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    @Override
    public void onNewToken(String token) {
        // Handle token for your purposes
        // ...
        // Also pass token to Optimove for registration
        Optimove.pushTokenStore(token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Handle message as you wish or forward this message to Optimove
        boolean wasHandledByOptimove = com.optimove.android.optimobile.FirebaseMessageHandler.onMessageReceived(this, remoteMessage);
        // Handle the message in case false is returned
    }
}

Note, that all issues with tracking conversion events (opens, dismissed, delivered) mentioned in overriding PushBroadcastReceiver section may also apply here.

Handling Push Action Buttons

Push messages allow you to hand-off to native application screens via deep-linking push action buttons. When tapped, these buttons pass control to the defined push action handler.

If you want to handle deep-links as part of a Push message you can create a class that implements the PushActionHandlerInterface and assign it during SDK initialization.

Optimove.getInstance().setPushActionHandler(new MyPushActionHandler());

A stub implementation of the handler could be as follows:

public class MyPushActionHandler implements PushActionHandlerInterface {
    public void handle(Context context, PushMessage pushMessage, String actionId){
       //- actionId is the button id you set when creating the notification
       //- Note, that when action button is clicked your app's activity is not launched -- you have to do it yourself in this handler.
    }
}

Changing the Push Icon

To change the icon shown in the status bar on Android, you can configure Optimove with a drawable at initialization time:

OptimoveConfig config = new OptimoveConfig.Builder(
    "<YOUR_OPTIMOVE_CREDENTIALS>","<YOUR_OPTIMOVE_MOBILE_CREDENTIALS>"
    ).setPushSmallIconId(R.id.my_push_small_icon)
    .build()

Optimove.initialize(this, config);

Make sure to comply with the status bar icon guidelines so the icon renders correctly on all devices. In particular, using non-alpha channel icons can lead to issues on some devices. For help preparing assets, we suggest checking out the Android Asset Studio

URL Pushes

Push notifications sent to open a URL will, by default, track the push open if the user taps the notification, and then open the default web browser.

⚠️ **GitHub.com Fallback** ⚠️