Leanplum's user guides and developer documentation.

Customize your push notifications [Sample: Android]

This article will show how to customize an Android Push Notification using Leanplum.

Setting the Push Notification Customizer

Sample project: Android sample projects here.

By default, it is possible to apply the Leanplum notification customizer once the notification is received in the app. This means that every notification will be by default customized once received in the app.

The LeanplumPushService.setCustomizer has to be placed inside the project Application class. In this way, we are basically telling our app to apply the customizer to every Push Notification we receive for the app. The customizer is passing the builder class which is used to set elements like icons or images.

Let's assume I'd like to change the smallIcon (included in the project Drawables folder) of my incoming Push Notifications.

I'd need to add the following code to the Application class:

LeanplumPushService.setCustomizer(new LeanplumPushNotificationCustomizer() {
@Override
public void customize(NotificationCompat.Builder builder, Bundle notificationPayload) {
// Setting a custom smallIcon included in the Drawable folder
builder.setSmallIcon(R.drawable.atest);
}
*   @Override
public void customize(Notification.Builder builder, Bundle notificationPayload, @Nullable Notification.Style notificationStyle) {
// leave this method empty
}
});

Or, if I wanted to support 2 lines of text in the BigPicture style, I'd need to pass true as the second argument of setCustomizer and include my custom code in the overloaded customize method.

LeanplumPushService.setCustomizer(new LeanplumPushNotificationCustomizer() {
  @Override
  public void customize(NotificationCompat.Builder builder, Bundle notificationPayload) {
    // leave this method empty
  }
  
@Override
public void customize(Notification.Builder builder, Bundle notificationPayload, @Nullable Notification.Style notificationStyle) {
    if (Build.VERSION.SDK_INT > 16) {
      if (notificationStyle != null && notificationStyle instanceof Notification.BigPictureStyle) {
        // Create largeIconBitmap
        ((Notification.BigPictureStyle) notificationStyle).bigLargeIcon(largeIconBitmap);
      }
    }
  }
}, true);

In general, within the LeanplumPushService.setCustomizer we can use what the NotificationCompat.Builder class could allow us to customize the Push Notification (see also Android docs here https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html).

Using the Notification payload

With the LeanplumPushService.setCustomizer we are also passing the Bundle notificationPayload object, which would come in hand if we want to get custom Data being included in the Push Notification payload.

In this way, we can grab key-value pairs being set in the Advanced options/Data fields in the Message composition in the Leanplum Dashboard.

A common use would be to set a String URL and use it to switch which images you want to be displayed in the Push Notification.

For example, if we'd like to customize the Push Notification Large Icon, we can pass the new Icon image via URL in the Push Notification payload by adding a String with the URL value in the Message on the Dashboard, like in the image:

Then, inside the customizer, we can get the largeImageURL value, retrieve the image and set it as the Large Icon in this way:

String urlLargeImageString = notificationPayload.getString("largeImageURL");

URL urlLargeImageURL = null;
try {
    urlLargeImageURL = new URL(urlLargeImageString);
} catch (MalformedURLException e) {
    e.printStackTrace();
}

Bitmap largeImage = null;
try {
    largeImage = BitmapFactory.decodeStream(urlLargeImageURL.openConnection().getInputStream());
} catch (java.io.IOException e) {
    e.printStackTrace();
}

builder.setLargeIcon(largeImage);

This approach is valid for also other customizable elements.

Sample project Notification images

Add titles to your push notifications

When you send a push notification to Android devices, Android automatically uses your app's name as the title of push notifications.

Using the customizer, you can customize a push notification title at the app level, meaning the title will automatically display in all future pushes sent from this app.

LeanplumPushService.setCustomizer(new LeanplumPushNotificationCustomizer() {
    @Override
    public void customize(NotificationCompat.Builder builder, Bundle bundle) {
        builder.setContentTitle("SAMPLE TITLE");
    }
    @Override
    public void customize(Notification.Builder builder, Bundle bundle, @Nullable Notification.Style style) {
        // leave this method empty
    }
});

A best practice is to keep push titles brief (5 words or fewer). Or, you can choose to have no title at all by leaving the title field empty.

Customize your push notifications [Sample: Android]


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.