Introduction
The Mobile Push Android SDK supports custom notification styles in two scenarios.
BasicCustomPushNotification: Uses the system's default notification style as a base to set styles, such as the notification method, status bar icon, and whether to create a notification if the application is in the foreground when a push message arrives.
AdvancedCustomPushNotification: Uses a custom notification style layout file as a base to set styles, such as the notification method, status bar icon, and whether to create a notification if the application is in the foreground when a push message arrives.
To use a custom notification style, you must complete two steps:
Step 1: Set the notification style on the client
Step 2: Specify the custom style when you push messages from the backend
Custom style notifications are supported in SDK V2.3.3 and later versions.
Alibaba Cloud Mobile Push for Android supports custom ringtones for the EMAS proprietary channel and certain third-party vendor channels. For more information about implementing this feature, see the following topic:
Step 1: Set the notification style on the client
Depending on your custom style scenario, use BasicCustomPushNotification or AdvancedCustomPushNotification.
Each style must correspond to a specific integer ID. If you set multiple styles to the same ID, the last style set takes effect.
This ID is required when you push from the backend. If the SDK cannot find a style with the corresponding ID when it creates a notification, it creates a notification with the default style.
You only need to set the style once. The SDK saves this setting and loads the corresponding style when needed.
BasicCustomPushNotification code sample
val notification = BasicCustomPushNotification()
notification.remindType = BasicCustomPushNotification.REMIND_TYPE_SOUND
notification.statusBarDrawable = R.drawable.logo_rounded_corner_120
val res = CustomNotificationBuilder.getInstance().setCustomNotification(1, notification)BasicCustomPushNotification notification = new BasicCustomPushNotification();
notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SOUND);
notification.setStatusBarDrawable(R.drawable.logo_rounded_corner_120);
boolean res = CustomNotificationBuilder.getInstance().setCustomNotification(1, notification);AdvancedCustomPushNotification code sample
val notification = AdvancedCustomPushNotification(
R.layout.notification_layout,
R.id.m_icon,
R.id.m_title,
R.id.m_text
)
notification.isServerOptionFirst = true
notification.isBuildWhenAppInForeground = false
val res = CustomNotificationBuilder.getInstance().setCustomNotification(2, notification)AdvancedCustomPushNotification notification = new AdvancedCustomPushNotification(R.layout.notification_layout, R.id.m_icon, R.id.m_title, R.id.m_text);
notification.setServerOptionFirst(true);
notification.setBuildWhenAppInForeground(false);
boolean res = CustomNotificationBuilder.getInstance().setCustomNotification(2, notification);Step 2: Specify the custom style when pushing messages from the backend
After you complete the client-side configuration, use OpenAPI on the server-side to specify the corresponding custom style ID when you push notifications.
Code sample
final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
final String date = dateFormat.format(new Date());
PushRequest pushRequest = new PushRequest();
// Push target
pushRequest.setAppKey(appKey);
pushRequest.setTarget("device"); // Push target. device: push to a device. account: push to a specified account. tag: push to a custom tag. all: push to all.
pushRequest.setTargetValue("deviceId");
// Push configuration
pushRequest.setType(1); // 0: message (default). 1: notification.
pushRequest.setTitle(date); // Message title
pushRequest.setBody("PushRequest body"); // Message body
pushRequest.setSummary("PushRequest summary"); // Notification summary
pushRequest.setAndroidNotificationBarType(2);// The ID of the notification style. The value range for custom notification bar styles is 0 to 100.
// Push configuration: Android
pushRequest.setAndroidOpenType("1"); // Action after tapping the notification. 1: open the application. 2: open an application Activity. 3: open a URL.
pushRequest.setAndroidExtParameters("{\"_NOTIFICATION_BAR_STYLE_\":\"2\"}");BasicCustomPushNotification
BasicCustomPushNotification lets you set the notification method, status bar icon, and whether to create a notification if the application is in the foreground when a push message arrives.
BasicCustomPushNotification
Default constructor. All configurations use default settings. The notification method uses vibration and sound. The `NotificationFlag` parameter uses `Notification.FLAG_AUTO_CANCEL`. The status bar icon uses `android.R.drawable.stat_notify_chat`.
API definition
public BasicCustomPushNotification()
Class
BasicCustomPushNotification
BasicCustomPushNotification
Constructor.
API definition
public BasicCustomPushNotification(int drawable, int flags, int remindType)
Class
BasicCustomPushNotification
Parameters
Parameter | Type | Required | Description |
drawable | int | Yes | Status bar icon |
flags | int | Yes | NotificationFlags. Supports the `Flag` parameter under the system `Notification`. |
remindType | int | Yes | Notification method type. The following options are supported:
|
Code sample
val customPushNotification = BasicCustomPushNotification(
R.drawable.logo,
Notification.FLAG_AUTO_CANCEL,
BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND
)
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification(R.drawable.logo, Notification.FLAG_AUTO_CANCEL, BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);setStatusBarDrawable
Sets the status bar icon for the custom notification.
API definition
public void setStatusBarDrawable(int statusBarDrawable)
Class
BasicCustomPushNotification
Metric Description
Parameter | Type | Required | Description |
statusBarDrawable | int | Yes | The resource ID of the status bar icon. |
Sample code
val customPushNotification = BasicCustomPushNotification()
customPushNotification.statusBarDrawable = R.drawable.logo
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setStatusBarDrawable(R.drawable.logo);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);setRemindType
Specifies the notification method for custom notifications.
API definition
public void setRemindType(int remindType)
Class
BasicCustomPushNotification
Parameters
Parameter | Type | Required | Description |
remindType | int | Yes | Notification method type. The following options are supported:
|
Code example
val customPushNotification = BasicCustomPushNotification()
customPushNotification.remindType = BasicCustomPushNotification.REMIND_TYPE_VIBRATE
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_VIBRATE);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);setNotificationFlags
Specifies the `flags` parameter for the custom notification.
API Definition
public void setNotificationFlags(int notificationFlags)
Class
BasicCustomPushNotification
Parameters
Parameter | Type | Required | Description |
notificationFlags | int | Yes | Supports the system's native `Notification Flag` parameter. |
Sample code
val customPushNotification = BasicCustomPushNotification()
customPushNotification.notificationFlags = Notification.FLAG_AUTO_CANCEL
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);setServerOptionFirst
Specifies the `serverOptionFirst` parameter for the custom notification.
You can set the notification method using OpenAPI or the Alibaba Cloud Mobile Push console. If the notification method set on the backend conflicts with the one specified in the custom style, the SDK determines the notification policy based on the `serverOptionFirst` parameter. If this parameter is `true`, the backend's notification method is used. Otherwise, the method specified in the custom style is used. The default value is `false`.
API Definition
public void setServerOptionFirst(boolean serverOptionFirst)
Class
BasicCustomPushNotification
Parameters
Parameter | Type | Required | Description |
serverOptionFirst | boolean | Yes | Specifies whether the server configuration takes precedence.
The default value is `false`. |
Code example
val customPushNotification = BasicCustomPushNotification()
customPushNotification.isServerOptionFirst = true
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setServerOptionFirst(true);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);setBuildWhenAppInForeground
Specifies whether to create a custom notification when a push message arrives while the application is in the foreground. By default, a notification is created.
API definition
public void setBuildWhenAppInForeground(boolean buildWhenAppInForeground)
Class
BasicCustomPushNotification
Parameters
Parameter | Type | Required | Description |
buildWhenAppInForeground | boolean | Yes | Specifies whether to create the notification. |
Sample code
val customPushNotification = BasicCustomPushNotification()
customPushNotification.isBuildWhenAppInForeground = true
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setBuildWhenAppInForeground(true);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);AdvancedCustomPushNotification
AdvancedCustomPushNotification is a child class of BasicCustomPushNotification. It inherits all methods from BasicCustomPushNotification and provides more configuration options.
AdvancedCustomPushNotification
Constructor. The AdvancedCustomPushNotification class does not have a default constructor.
API definition
public AdvancedCustomPushNotification( int view, int iconViewId, int titleViewId, int contentViewId)
Class
AdvancedCustomPushNotification
Metric Description
Parameter | Type | Required | Description |
view | int | Yes | The ID of the custom notification layout file. Note The custom layout for a `Notification` is a `RemoteViews`. Like other `RemoteViews`, only `FrameLayout`, `LinearLayout`, and `RelativeLayout` layouts are supported in the custom view layout file. |
iconViewId | int | Yes | The `viewId` of the icon in the custom layout file. |
titleViewId | int | Yes | The `viewId` of the title in the custom layout file. |
contentViewId | int | Yes | The `viewId` for displaying the notification body in the custom layout file. |
Code sample
val customPushNotification = AdvancedCustomPushNotification(
R.layout.demo_notification_cus_notif,
R.id.m_icon,
R.id.m_title,
R.id.m_text
)
CustomNotificationBuilder.getInstance().setCustomNotification(2, customPushNotification)AdvancedCustomPushNotification customPushNotification = new AdvancedCustomPushNotification(R.layout.demo_notification_custom_notif, R.id.m_icon, R.id.m_title, R.id.m_text);
CustomNotificationBuilder.getInstance().setCustomNotification(2, customPushNotification);Example: demo_notification_cus_notif.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/m_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Advanced Notification"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:textSize="10sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toRightOf="@id/m_icon"
android:layout_toLeftOf="@id/text"
android:orientation="vertical">
<TextView
android:id="@+id/m_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#2844DD"
android:text="title"
android:textSize="20sp" />
<TextView
android:id="@+id/m_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#AA3b5E"
android:text="text"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>setIcon
Sets the icon that is displayed in the notification bar. This icon is displayed in the control specified by `iconViewId`.
API definition
public void setIcon(int icon)
Class
AdvancedCustomPushNotification
Parameters
Parameter | Type | Required | Description |
icon | int | Yes | The resource ID of the icon. |
Code sample
val customPushNotification = AdvancedCustomPushNotification(
R.layout.demo_notification_cus_notif,
R.id.m_icon,
R.id.m_title,
R.id.m_text
)
customPushNotification.icon = R.id.m_icon
CustomNotificationBuilder.getInstance().setCustomNotification(2, customPushNotification)AdvancedCustomPushNotification customPushNotification = new AdvancedCustomPushNotification(R.layout.demo_notification_cus_notif, R.id.m_icon, R.id.m_title, R.id.m_text);
customPushNotification.setIcon(R.id.m_icon);
CustomNotificationBuilder.getInstance().setCustomNotification(2, customPushNotification);