Custom notification styles API (4.x)
This article covers BasicCustomPushNotification, AdvancedCustomPushNotification, CustomNotificationBuilder, and the notification creation extension interface in Android SDK 4.x.
The custom notification style APIs in this topic apply only to notifications from the ACCS channel, not to those from third-party channels.
This topic covers Mobile Push Android SDK 4.x. The APIs and examples are based on version 4.0.0. For applications using the 3.x SDK, refer to the 3.x API documentation.
Usage and workflow
Configure styles for vendor channel notifications according to the specific vendor's capabilities and configuration methods.
Initialize the SDK and register a style ID on the client. When the server sends a push, specify the same style ID using AndroidNotificationBarType. If a style with the specified ID is not found, the SDK uses the default style. If multiple styles are registered with the same ID, the most recent registration overwrites the previous one.
The classes are in the com.alibaba.sdk.android.push.notification package. In the following examples, R.drawable and R.layout are placeholder resources and you must replace them with your application's actual resources. Any custom layout must meet the system requirements for RemoteViews.
The examples below demonstrate each configuration method. A setter method on a style object only modifies that object. To apply the updated configuration, call setCustomNotification to re-register the same object and style ID. The corresponding sections specify the origin of variables used in the examples, such as style and pushData.
Register a basic style
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);
if (!saved) {
// Check the initialization, the style ID, and the style object.
}val style = BasicCustomPushNotification()
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)On the server-side, set AndroidNotificationBarType to 1. The 1 in this example is the style ID you registered, not a BasicCustomPushNotification.NOTIFICATION_TYPE_* value.
BasicCustomPushNotification constructors
public BasicCustomPushNotification();
public BasicCustomPushNotification(int drawable, int flags, int remindType);
|
Parameter |
Type |
Required |
Description |
|
drawable |
int |
Yes |
The drawable resource ID for the status bar icon, for example, |
|
flags |
int |
Yes |
The Android Notification flag. You can combine multiple flags using bitwise operations, for example, |
|
remindType |
int |
Yes |
The reminder type. Valid values are |
The no-argument constructor sets the status bar icon to android.R.drawable.stat_notify_chat, the reminder type to sound and vibration, and the flags value to Notification.FLAG_AUTO_CANCEL. By default, notifications are shown in the foreground (true), and prioritization of the server-side reminder type is disabled (false).
|
Constant |
Value and description |
|
REMIND_TYPE_SILENT |
0: Silent |
|
REMIND_TYPE_VIBRATE |
1: Vibrate |
|
REMIND_TYPE_SOUND |
2: Sound |
|
REMIND_TYPE_VIBRATE_AND_SOUND |
3: Sound and vibrate |
For Android 8.0 and later, notification effects such as sound and vibration are determined by both the NotificationChannel configuration and the user's system notification settings.
BasicCustomPushNotification(): Create default style
Creates a basic style object with default configurations. The style is not automatically registered.
API definition
public BasicCustomPushNotification();
Parameters: None.
Return value: Returns a BasicCustomPushNotification object. This is a synchronous call with no callback.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();val style = BasicCustomPushNotification()BasicCustomPushNotification(int, int, int): Set initial configurations
Sets the status bar icon, notification flags, and reminder type. Other configurations use their default values.
API definition
public BasicCustomPushNotification(int drawable, int flags, int remindType);
Parameter details
|
Parameter |
Type |
Required |
Description |
|
drawable |
int |
Yes |
The drawable resource ID for the status bar icon, for example, |
|
flags |
int |
Yes |
The Android Notification flag. You can combine multiple flags using bitwise operations, for example, |
|
remindType |
int |
Yes |
The reminder type. Valid values are |
Return value: Returns a BasicCustomPushNotification object. You must register this object separately to use it in notifications.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification(
R.drawable.notification_small,
Notification.FLAG_AUTO_CANCEL,
BasicCustomPushNotification.REMIND_TYPE_SOUND);val style = BasicCustomPushNotification(
R.drawable.notification_small,
Notification.FLAG_AUTO_CANCEL,
BasicCustomPushNotification.REMIND_TYPE_SOUND
)BasicCustomPushNotification methods
|
Method |
Description |
|
int getStatusBarDrawable() |
Returns the resource ID of the status bar icon. |
|
void setStatusBarDrawable(int statusBarDrawable) |
Sets the status bar icon using a drawable resource ID. |
|
int getRemindType() |
Returns the |
|
void setRemindType(int remindType) |
Sets one of the four valid remind types. Invalid values are ignored and do not affect the current configuration. |
|
int getNotificationFlags() |
Returns the Android |
|
void setNotificationFlags(int notificationFlags) |
Sets the Android |
|
boolean isServerOptionFirst() |
Checks if server-side remind types have priority. Default: |
|
void setServerOptionFirst(boolean first) |
If |
|
boolean isBuildWhenAppInForeground() |
Checks if a notification is created when the app is in the foreground. Default: |
|
void setBuildWhenAppInForeground(boolean enabled) |
If |
|
int getNotificationType() |
Returns the style type: |
|
String toString() |
Returns a string describing the style configuration for debugging. |
In SDK v4.x, when a notification is not displayed in the foreground, the SDK still delivers data to the app through the onNotification callback. The onNotificationReceivedInApp callback is no longer triggered. If your app overrides showNotificationNow, ensure you retain the necessary default display logic.
getStatusBarDrawable
Gets the status bar icon. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public int getStatusBarDrawable();
Parameters: None.
Return value: The drawable resource ID of the current style.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
int drawable = style.getStatusBarDrawable();val style = BasicCustomPushNotification()
val drawable = style.getStatusBarDrawable()setStatusBarDrawable
Sets the status bar icon. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public void setStatusBarDrawable(int statusBarDrawable);
Parameters
|
Parameter |
Type |
Required |
Description |
|
statusBarDrawable |
int |
Yes |
The drawable resource ID for the status bar icon. This is not the view ID of a view within the notification layout. |
Return value: None. This method modifies the style object in place.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
style.setStatusBarDrawable(R.drawable.notification_small);
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);val style = BasicCustomPushNotification()
style.setStatusBarDrawable(R.drawable.notification_small)
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)getRemindType
Gets the remind type. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public int getRemindType();
Parameters: None.
Return value: The current REMIND_TYPE_* constant, with a value of 0, 1, 2, or 3.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
int remindType = style.getRemindType();val style = BasicCustomPushNotification()
val remindType = style.getRemindType()setRemindType
Sets the remind type. This method applies to BasicCustomPushNotification and advanced styles that extend it. On Android 8.0 and higher, the actual sound and vibration also depend on the NotificationChannel configuration.
Method signature
public void setRemindType(int remindType);
Parameters
|
Parameter |
Type |
Required |
Description |
|
remindType |
int |
Yes |
One of the four |
Return value: None.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
style.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SOUND);
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);val style = BasicCustomPushNotification()
style.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SOUND)
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)getNotificationFlags
Gets the notification flags. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public int getNotificationFlags();
Parameters: None.
Return value: The current Android Notification flags.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
int flags = style.getNotificationFlags();val style = BasicCustomPushNotification()
val flags = style.getNotificationFlags()setNotificationFlags
Sets the notification flags. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public void setNotificationFlags(int notificationFlags);
Parameters
|
Parameter |
Type |
Required |
Description |
|
notificationFlags |
int |
Yes |
An Android |
Return value: None. This method modifies the style object in place.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
style.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);val style = BasicCustomPushNotification()
style.setNotificationFlags(Notification.FLAG_AUTO_CANCEL)
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)isServerOptionFirst
Gets the priority source for the remind type. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public boolean isServerOptionFirst();
Parameters: None.
Return value: true if server-side settings take priority; false if client-side settings do. Default: false.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean serverFirst = style.isServerOptionFirst();val style = BasicCustomPushNotification()
val serverFirst = style.isServerOptionFirst()setServerOptionFirst
Sets the priority source for the remind type. This method applies to BasicCustomPushNotification and advanced styles that extend it. Use this option to control whether client-side or server-side settings determine the remind type.
Method signature
public void setServerOptionFirst(boolean first);
Parameters
|
Parameter |
Type |
Required |
Description |
|
first |
boolean |
Yes |
|
Return value: None.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
style.setServerOptionFirst(true);
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);val style = BasicCustomPushNotification()
style.setServerOptionFirst(true)
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)isBuildWhenAppInForeground
Gets the foreground notification behavior. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public boolean isBuildWhenAppInForeground();
Parameters: None.
Return value: true if notifications of this style can be created when the app is in the foreground; false otherwise. Default: true.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean enabled = style.isBuildWhenAppInForeground();val style = BasicCustomPushNotification()
val enabled = style.isBuildWhenAppInForeground()setBuildWhenAppInForeground
Sets the foreground notification behavior. This method applies to BasicCustomPushNotification and advanced styles that extend it. Even when this setting is disabled, the app still receives data through the onNotification callback. If you override showNotificationNow, ensure you preserve any necessary default display logic.
Method signature
public void setBuildWhenAppInForeground(boolean enabled);
Parameters
|
Parameter |
Type |
Required |
Description |
|
enabled |
boolean |
Yes |
|
Return value: None.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
style.setBuildWhenAppInForeground(false);
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);val style = BasicCustomPushNotification()
style.setBuildWhenAppInForeground(false)
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)getNotificationType
Gets the style type. This method applies to BasicCustomPushNotification and advanced styles that extend it.
Method signature
public int getNotificationType();
Parameters: None.
Return value: NOTIFICATION_TYPE_CUSTOM_BASIC (2) for a basic style or NOTIFICATION_TYPE_CUSTOM_ADVANCED (3) for an advanced style. This value is not the registered style ID.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
int notificationType = style.getNotificationType();val style = BasicCustomPushNotification()
val notificationType = style.getNotificationType()toString
Gets a string representation of the style's configuration for debugging. This method applies to BasicCustomPushNotification and advanced styles that extend it. To persist a style, use the setCustomNotification() method.
Method signature
public String toString();
Parameters: None.
Return value: A string representation of the style's configuration.
Code sample
BasicCustomPushNotification style = new BasicCustomPushNotification();
String description = style.toString();val style = BasicCustomPushNotification()
val description = style.toString()AdvancedCustomPushNotification constructor
AdvancedCustomPushNotification extends BasicCustomPushNotification and customizes the notification layout. It inherits the configuration methods from the parent class and does not have a no-argument constructor.
public AdvancedCustomPushNotification(int view, int iconViewId,
int titleViewId, int contentViewId);
|
Parameter |
Type |
Required |
Description |
|
view |
int |
Yes |
The |
|
iconViewId |
int |
Yes |
The |
|
titleViewId |
int |
Yes |
The |
|
contentViewId |
int |
Yes |
The |
Create res/layout/push_notification_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="@+id/push_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@null" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/push_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/push_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
The constructor creates an AdvancedCustomPushNotification object. You must then register the style by calling setCustomNotification().
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)The setIcon() method accepts an image resource ID (e.g., R.drawable), not the R.id of an ImageView. System versions may restrict the display area and styling of custom layouts, so test your implementation on the target systems.
AdvancedCustomPushNotification methods
|
Definition |
Description |
|
int getNotificationView() |
Returns the layout resource ID. |
|
int getIconView() |
Returns the icon view ID. |
|
int getTitleView() |
Returns the title view ID. |
|
int getContentView() |
Returns the content view ID. |
|
int getIcon() |
Returns the icon image resource ID. |
|
void setIcon(int icon) |
Sets the image for the icon view. The parameter is a drawable resource ID. |
|
String toString() |
Returns a description of the style. |
Reminder type, status bar icon, flags, server-side priority, and foreground display settings inherit from BasicCustomPushNotification.
getNotificationView
Gets the layout resource ID.
Definition
public int getNotificationView();
Parameters: None.
Return value: Returns the layout resource ID passed to the constructor.
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
int value = style.getNotificationView();val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
val value = style.getNotificationView()getIconView
Gets the icon view ID.
Definition
public int getIconView();
Parameters: None.
Return value: Returns the icon view ID passed to the constructor. This is not an image resource ID.
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
int value = style.getIconView();val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
val value = style.getIconView()getTitleView
Gets the title view ID.
Definition
public int getTitleView();
Parameters: None.
Return value: Returns the title TextView ID passed to the constructor.
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
int value = style.getTitleView();val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
val value = style.getTitleView()getContentView
Gets the content view ID.
Definition
public int getContentView();
Parameters: None.
Return value: Returns the content TextView ID passed to the constructor.
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
int value = style.getContentView();val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
val value = style.getContentView()getIcon
Gets the icon image resource ID.
Definition
public int getIcon();
Parameters: None.
Return value: Returns the drawable resource ID set by setIcon(). Returns 0 if no icon is set.
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
int value = style.getIcon();val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
val value = style.getIcon()setIcon
Sets the icon image.
Definition
public void setIcon(int icon);
Parameters
|
Parameter |
Type |
Required |
Description |
|
icon |
int |
Yes |
The drawable resource ID to display in the icon view, for example, |
Return value: None. This method does not set the small status bar icon.
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
style.setIcon(R.drawable.notification_large);
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(3, style);val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
style.setIcon(R.drawable.notification_large)
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(3, style)toString
Returns the advanced style description.
Definition
public String toString();
Parameters: None.
Return value: Returns a string describing the basic and layout configurations, intended for debugging. To persist the style, use setCustomNotification().
Examples
AdvancedCustomPushNotification style = new AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content);
String value = style.toString();val style = AdvancedCustomPushNotification(
R.layout.push_notification_custom,
R.id.push_icon, R.id.push_title, R.id.push_content
)
val value = style.toString()CustomNotificationBuilder
public static CustomNotificationBuilder getInstance();
public boolean setCustomNotification(int customNotificationId,
BasicCustomPushNotification notification);
|
Parameter |
Description |
|
customNotificationId |
A style ID greater than 0. This must be consistent with the ID used for AndroidNotificationBarType on the server side. |
|
notification |
A non-null BasicCustomPushNotification object or an instance of its subclass, AdvancedCustomPushNotification. |
|
boolean return value |
|
Call PushServiceFactory.init before registering a style. The SDK saves the style configuration and loads it on demand. If a style changes, register it again using the same ID.
getInstance
Gets the CustomNotificationBuilder singleton, which you can use to register styles.
Interface definition:
public static CustomNotificationBuilder getInstance();
Parameters: None.
Return value: The CustomNotificationBuilder singleton object.
Code example:
CustomNotificationBuilder builder = CustomNotificationBuilder.getInstance();val builder = CustomNotificationBuilder.getInstance()setCustomNotification
Registers a style object for a specified ID. Registering a new style with the same ID overwrites the previous configuration.
Interface definition:
public boolean setCustomNotification(int customNotificationId, BasicCustomPushNotification notification);
Parameters
|
Parameter |
Type |
Required |
Description |
|
customNotificationId |
int |
Yes |
A style ID greater than 0. This must be consistent with the AndroidNotificationBarType on the server side. |
|
notification |
BasicCustomPushNotification |
Yes |
A non-null BasicCustomPushNotification object or an instance of its subclass, AdvancedCustomPushNotification. |
Return value: A boolean. Returns true on successful registration, or false if the SDK has not been initialized, the parameters are invalid, or a serialization error occurs.
Code example:
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);
if (!saved) {
// Check SDK initialization, the style ID, and the style object.
}val style = BasicCustomPushNotification()
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)
if (!saved) {
// Check SDK initialization, the style ID, and the style object.
}Adjust notification builds with push data
In Android SDK 4.x, the supported callbacks for MessageReceiver and AliyunMessageIntentService are as follows:
ACCS and FCM channels: Support all business callbacks, including message reception (
onMessage), notification arrival (onNotification), notification open (onNotificationOpened), and notification removal (onNotificationRemoved).Other Android vendor channels (excluding FCM): Only support the notification open (
onNotificationOpened) callback. Other callbacks, such as message reception, notification arrival, and notification removal, are not triggered.
To further configure a notification when the SDK creates it, override the hookNotificationBuild() method in your custom MessageReceiver or AliyunMessageIntentService to return a NotificationConfigure object. The methods in this object correspond to the system builder, the compatibility builder, and the final built Notification.
public NotificationConfigure hookNotificationBuild();
public Notification customNotificationUI(Context context, PushData pushData);
If customNotificationUI(Context, PushData) returns a non-null value, the SDK uses the notification returned by your application. If it returns null, the SDK continues with its default build process. When you build the notification yourself, ensure that you correctly handle system notification channels and display requirements.
|
PushData getters |
Description |
|
getTitle()/getContentText() |
The notification title and content text. |
|
getImage()/getSound()/getNotifyType() |
The image, sound, and notification type. |
|
getExtraMap() |
The extra parameters for your business logic. Check for null before use. |
|
getNotificationChannel()/getPriority() |
The notification channel ID and priority. |
|
getCustomNotificationId()/getStyle() |
The custom style ID and the server-side style field. |
|
getBigTitle()/getBigBody()/getBigPicture()/getInboxContent() |
Parameters related to big text, big picture, or inbox-style notifications. |
|
getNotifyId()/getMsgId() |
The integer notification ID and the server message ID, which have different meanings. |
Place the following override examples in your custom MessageReceiver or AliyunMessageIntentService, and retain the four required business callbacks for the class. The methods of NotificationConfigure should be implemented within the object returned by hookNotificationBuild. The SDK passes in the PushData object, and all subsequent examples use it to read data.
hookNotificationBuild: Notification build configuration
Return a NotificationConfigure implementation. The SDK calls the methods in this implementation at the corresponding stages of the default build process.
Method signature
public NotificationConfigure hookNotificationBuild();
Parameters: None.
Return value: A NotificationConfigure object. If this method returns null, this extension configuration is not applied. When customNotificationUI returns a custom-built notification, the SDK skips these configuration stages of the default build process.
Code example
@Override
public NotificationConfigure hookNotificationBuild() {
return new NotificationConfigure() {
@Override
public void configBuilder(Notification.Builder builder, PushData data) {
builder.setAutoCancel(true);
}
@Override
public void configBuilder(androidx.core.app.NotificationCompat.Builder builder,
PushData data) {
builder.setAutoCancel(true);
}
@Override
public void configNotification(Notification notification, PushData data) {
// You can read data and adjust the final Notification based on your business requirements.
}
};
}override fun hookNotificationBuild(): NotificationConfigure {
return object : NotificationConfigure {
override fun configBuilder(builder: Notification.Builder, data: PushData) {
builder.setAutoCancel(true)
}
override fun configBuilder(
builder: androidx.core.app.NotificationCompat.Builder, data: PushData
) {
builder.setAutoCancel(true)
}
override fun configNotification(notification: Notification, data: PushData) {}
}
}customNotificationUI: Return a custom notification
Override this method to replace the SDK's default notification. The SDK then displays the returned Notification object.
Method signature
public Notification customNotificationUI(Context context, PushData pushData);
Parameters
|
Parameter |
Type |
Required |
Description |
|
context |
Context |
Yes |
The context used by the SDK to create the notification. |
|
pushData |
PushData |
No |
The notification data parsed by the SDK. The value may be |
Return value: Notification. If non-null, the SDK uses the returned notification. If null, the SDK continues with its default build process.
This example falls back to the SDK's default build process if a notification channel ID is not provided. For Android 8.0 and later, you must create the required NotificationChannel in advance and ensure that your application has system notification permissions.
Code example
@Override
public Notification customNotificationUI(Context context, PushData pushData) {
if (pushData == null || android.text.TextUtils.isEmpty(pushData.getNotificationChannel())) {
return null;
}
// The corresponding notification channel should already be created by the app. Replace sample resources with your actual resources.
return new androidx.core.app.NotificationCompat.Builder(context, pushData.getNotificationChannel())
.setSmallIcon(R.drawable.notification_small)
.setContentTitle(pushData.getTitle())
.setContentText(pushData.getContentText())
.setAutoCancel(true)
.build();
}override fun customNotificationUI(context: Context, pushData: PushData?): Notification? {
val channelId = pushData?.notificationChannel
if (pushData == null || channelId.isNullOrEmpty()) return null
// The corresponding notification channel should already be created by the app.
return androidx.core.app.NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.notification_small)
.setContentTitle(pushData.title)
.setContentText(pushData.contentText)
.setAutoCancel(true)
.build()
}configBuilder(Notification.Builder): Configure the system builder
The SDK calls this overload when it uses the system builder to build a notification. When it uses the compatibility builder, it calls the other overload.
Method signature
void configBuilder(Notification.Builder builder, PushData pushData);
Parameters
|
Parameter |
Type |
Required |
Description |
|
builder |
Notification.Builder |
Yes |
The notification builder the SDK is using. Modify this object directly within the callback. |
|
pushData |
PushData |
No |
The parsed data for the current notification. Check for null before reading optional fields. |
Return value: None (void). Modify the builder object directly. The SDK then uses the modified builder to create the notification.
Replace the corresponding override in the NotificationConfigure implementation above with this example. You still need to implement the other two methods.
Code example
@Override
public void configBuilder(Notification.Builder builder, PushData pushData) {
builder.setAutoCancel(true);
}override fun configBuilder(builder: Notification.Builder, pushData: PushData?) {
builder.setAutoCancel(true)
}configBuilder(androidx.core.app.NotificationCompat.Builder): Configure the compatibility builder
The SDK calls this overload when it uses the compatibility builder to build a notification. When it uses the system builder, it calls the other overload.
Method signature
void configBuilder(androidx.core.app.NotificationCompat.Builder builder, PushData pushData);
Parameters
|
Parameter |
Type |
Required |
Description |
|
builder |
androidx.core.app.NotificationCompat.Builder |
Yes |
The notification builder the SDK is using. Modify this object directly within the callback. |
|
pushData |
PushData |
No |
The parsed data for the current notification. Check for null before reading optional fields. |
Return value: None (void). Modify the builder object directly. The SDK then uses the modified builder to create the notification.
Replace the corresponding override in the NotificationConfigure implementation above with this example. You still need to implement the other two methods.
Code example
@Override
public void configBuilder(androidx.core.app.NotificationCompat.Builder builder, PushData pushData) {
builder.setAutoCancel(true);
}override fun configBuilder(builder: androidx.core.app.NotificationCompat.Builder, pushData: PushData?) {
builder.setAutoCancel(true)
}configNotification: Adjust the built notification
Adjust the Notification object after the SDK finishes its build process.
Method signature
void configNotification(Notification notification, PushData pushData);
Parameters
|
Parameter |
Type |
Required |
Description |
|
notification |
Notification |
Yes |
The |
|
pushData |
PushData |
No |
The parsed data for the current notification. Check for null before reading optional fields. |
Return value: None (void). Modify the passed notification object directly. The SDK then displays it.
Replace the corresponding method in the NotificationConfigure implementation above with this example. You still need to implement the two configBuilder overrides.
Code example
@Override
public void configNotification(Notification notification, PushData pushData) {
notification.flags |= Notification.FLAG_AUTO_CANCEL;
}override fun configNotification(notification: Notification, pushData: PushData?) {
notification.flags = notification.flags or Notification.FLAG_AUTO_CANCEL
}PushData.getTitle: Notification title
Gets the notification title from the PushData object.
Method signature
public String getTitle();
Parameters: None.
Return value: The notification title.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getTitle();
}val value = pushData?.getTitle()PushData.getContentText: Notification content
Gets the notification content text from the PushData object.
Method signature
public String getContentText();
Parameters: None.
Return value: The notification content text.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getContentText();
}val value = pushData?.getContentText()PushData.getImage: Notification image
Gets the notification image from the PushData object.
Method signature
public String getImage();
Parameters: None.
Return value: The notification image field, such as an image URL. This is not a Bitmap object. The value may be null.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getImage();
}val value = pushData?.getImage()PushData.getSound: Sound field
Gets the sound field from the PushData object.
Method signature
public String getSound();
Parameters: None.
Return value: The sound field from the payload. The value may be null. The actual sound played depends on the notification configuration and system settings.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getSound();
}val value = pushData?.getSound()PushData.getNotifyType: Notification type
Gets the notification type from the PushData object.
Method signature
public int getNotifyType();
Parameters: None.
Return value: NOTIFY_TYPE_SILENT (0), NOTIFY_TYPE_VIBRATE (1), NOTIFY_TYPE_SOUND (2), or NOTIFY_TYPE_VIBRATE_SOUND (3).
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
int value = pushData.getNotifyType();
}val value = pushData?.getNotifyType()PushData.getExtraMap: Extra parameters
Gets the extra parameters from the PushData object.
Method signature
public Map<String, String> getExtraMap();
Parameters: None.
Return value: A Map of extra parameters parsed from the original notification's ext JSON field. The value may be null. The key names and their business meanings are defined by the sender.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
Map<String, String> value = pushData.getExtraMap();
}val value = pushData?.getExtraMap()PushData.getNotificationChannel: Notification channel
Gets the notification channel from the PushData object.
Method signature
public String getNotificationChannel();
Parameters: None.
Return value: The notification channel ID. The value may be null. This method does not create a NotificationChannel.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getNotificationChannel();
}val value = pushData?.getNotificationChannel()PushData.getPriority: Notification priority
Gets the notification priority from the PushData object.
Method signature
public int getPriority();
Parameters: None.
Return value: The notification priority, typically from -2 to 2. If not specified, the default priority is 0. For Android 8.0 and later, the notification's appearance is also affected by the channel's importance level.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
int value = pushData.getPriority();
}val value = pushData?.getPriority()PushData.getCustomNotificationId: Custom style ID
Gets the custom style ID from the PushData object.
Method signature
public int getCustomNotificationId();
Parameters: None.
Return value: The ID of the client-registered style selected by the server. If not specified, returns NO_CUSTOM_NOTIFICATION.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
int value = pushData.getCustomNotificationId();
}val value = pushData?.getCustomNotificationId()PushData.getStyle: Server-side style field
Gets the server-side style field from the PushData object.
Method signature
public String getStyle();
Parameters: None.
Return value: The server-side style field. The value may be null. This field is different from the registered style ID.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getStyle();
}val value = pushData?.getStyle()PushData.getBigTitle: Big style title
Gets the big style title from the PushData object.
Method signature
public String getBigTitle();
Parameters: None.
Return value: The title field for a big text or big picture notification. The value may be null.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getBigTitle();
}val value = pushData?.getBigTitle()PushData.getBigBody: Big text body
Gets the big text body from the PushData object.
Method signature
public String getBigBody();
Parameters: None.
Return value: The body text field for a big text notification. The value may be null.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getBigBody();
}val value = pushData?.getBigBody()PushData.getBigPicture: Big picture field
Gets the big picture field from the PushData object.
Method signature
public String getBigPicture();
Parameters: None.
Return value: The image field for a big picture notification, which is not a downloaded Bitmap. The value may be null.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getBigPicture();
}val value = pushData?.getBigPicture()PushData.getInboxContent: Inbox-style content
Gets the inbox-style notification content from the PushData object.
Method signature
public String getInboxContent();
Parameters: None.
Return value: The server-side content field for an inbox-style notification. The value may be null. This method does not convert the content into a list control.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getInboxContent();
}val value = pushData?.getInboxContent()PushData.getNotifyId: Integer notification ID
Gets the integer notification ID from the PushData object.
Method signature
public int getNotifyId();
Parameters: None.
Return value: The integer ID used for the Android notification. This is not the server message ID. This method returns 0 if the ID is missing or parsing fails.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
int value = pushData.getNotifyId();
}val value = pushData?.getNotifyId()PushData.getMsgId: Server message ID
Gets the server message ID from the PushData object.
Method signature
public String getMsgId();
Parameters: None.
Return value: The server message ID. The value may be null. Do not use this value as the integer notification ID.
Place this example in a notification build callback that has a pushData parameter. This method itself does not change the notification style.
Code example
if (pushData != null) {
String value = pushData.getMsgId();
}val value = pushData?.getMsgId()Verification
Test the default style, a registered style, an unregistered ID, suppression of foreground notifications, clicks through the ACCS proprietary channel, notification channels on Android 8.0 or higher, and the system notification permission.
Related documents
For integration instructions, see Android SDK integration (4.x).
If your application uses version 3.x, see custom notification style interface (3.x).