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:

1244

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");
if (urlLargeImageString != null) {
    try {
        URL urlLargeImageURL = new URL(urlLargeImageString);
        URLConnection connection = urlLargeImageURL.openConnection();
        Bitmap largeImage = BitmapFactory.decodeStream(connection.getInputStream());
        builder.setLargeIcon(largeImage);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This approach is valid for also other customizable elements.

Sample project Notification images

947 1080