Skip to content

Interactive notifications

Tereza Juric edited this page Aug 9, 2017 · 39 revisions

Interactive notifications are push notifications that provide an option for end user to interact with application through button tap action. This interaction can be accomplished by using our predefined categories or creating your own.

Feature is provided since MM SDK version 1.6.16 but we recommend using 1.7.0 since we separated it to another module.

Each category has its own ID and actions with their IDs. mm_ prefix is reserved for all predefined IDs and cannot be used for custom actions. Actions may bring app to foreground or leave it in the background.

Starting in Android N, actions are shown without icons in order to accommodate more text. An icon should still be provided because devices with earlier versions of the OS continue to rely on it, as will Android Wear and Android Auto devices.

Configure project to use interactive notifications

  1. Add dependency to build.gradle file. Interactive module requires core module library to be included, so following configuration should be the right one:
compile ('org.infobip.mobile.messaging.api:infobip-mobile-messaging-android-interactive-sdk:1.7.0@aar')
compile ('org.infobip.mobile.messaging.api:infobip-mobile-messaging-android-sdk:1.7.0@aar') {
    transitive = true;
}

Check latest version here.

Notice

All required manifest components are merged to application manifest automatically by manifest merger. Please include interactive-related components to manifest manually if manifest merger was disabled.

  1. Subscribe to NOTIFICATION_ACTION_TAPPED event and you're all set up to test predefined categories. In the event you've subscribed to you can check which action has been chosen and act upon it:
// declare action tapped message receiver
private final BroadcastReceiver actionTappedMessageReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Message message = Message.createFrom(intent.getExtras());
        NotificationAction action = NotificationAction.createFrom(intent.getExtras());
        NotificationCategory notificationCategory = NotificationCategory.createFrom(intent.getExtras());

        if (action != null && "mm_accept".equals(action.getId()) {
            // message.getCategory() should be the same as notificationCategory.getId()
            // TODO
        }
    }
};

@Override
protected void onResume() {
    super.onResume();
    // subscribe to NOTIFICATION_ACTION_TAPPED event
    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
    localBroadcastManager.registerReceiver(actionTappedMessageReceiver, new IntentFilter(InteractiveEvent.NOTIFICATION_ACTION_TAPPED.getKey()));
}
  1. Use setNotificationCategories method to subscribe to custom categories

Send interactive notification

Interactive notifications can be tested through our Single PUSH message and Multiple PUSH messages APIs by using category parameter.

Predefined categories

Mobile Messaging SDK provides one predefined interactive category for now, but this list will be extended.

Display of interactive notifications with predefined categories in notification center can be tested by including interactive module as described in the first step and sending message through Push API. Action handling needs to be implemented as described in the second step.

A = action

Category ID Action ID A - Title A - Foreground
mm_accept_decline mm_accept Accept true
mm_decline Decline false

Note

Mobile Messaging SDK provides localization for action button titles of predefined categories. Titles are localized for the following languages: Arabic, Czech, Danish, German, English, Spanish (Latin America), Spanish, Finnish, French, Hebrew, Croatian, Hungarian, Indonesian, Italian, Japanese, Korean, Malay, Norwegian, Dutch, Polish, Portuguese (Portugal), Portuguese, Romanian, Russian, Slovak, Swedish, Thai, Turkish, Vietnamese, Chinese (Simplified), Chinese (Traditional), Hindi.

Custom categories

Setting up custom categories enables you to configure interactive notification categories along with their actions.

Maximum of three (3) actions are shown in the default notification layout. Actions are displayed in the order they've been set (in current example, "cancel" action will be on the left, "share" action will be on the right).

NotificationAction cancelAction = new NotificationAction.Builder()
        .withId("cancel")
        .withTitleResourceId(R.string.cancel)
        .withIcon(R.drawable.cancel)
        .build();
          
NotificationAction shareAction = new NotificationAction.Builder()
        .withId("share")
        .withTitleResourceId(R.string.share)
        .withIcon(R.drawable.share)
        .withBringingAppToForeground(true)
        .build();
          
NotificationCategory notificationCategory = new NotificationCategory("category_share", cancelAction, shareAction);
new MobileInteractive.getInstance(context).setNotificationCategories(notificationCategory);
Clone this wiki locally