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 project 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 being 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. 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);

}
});

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 however 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'd like to have it 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

 


Was this article helpful?
Have more questions? Submit a request