本文介绍 Android SDK 4.x 的 BasicCustomPushNotification、AdvancedCustomPushNotification、CustomNotificationBuilder,以及通知创建扩展接口。
本文中的自定义通知样式接口仅对 ACCS 自有通道通知生效,对厂商通道通知无效。
适用于移动推送 Android SDK 4.x,本文接口和示例以已发布的 4.0.0 版本为基线。使用 3.x 的应用请参见对应的 3.x API 文档。
使用范围和流程
厂商通道通知的样式请按对应厂商的能力和配置方式设置。
先初始化 SDK,再在客户端注册样式 ID;服务端推送时通过 AndroidNotificationBarType 指定相同样式 ID。若未找到对应 ID 的样式,SDK 使用默认样式。多个样式使用相同 ID 时,最后一次注册覆盖前一次。
类位于 com.alibaba.sdk.android.push.notification。以下 R.drawable 和 R.layout 为示例资源,需替换成应用实际资源;自定义布局必须满足系统 RemoteViews 要求。
下文每个设置方法单独示例。样式对象上的 setter 仅修改该对象;要应用更新后的配置,调用 setCustomNotification 注册同一对象和样式 ID。示例中的 style、pushData 等变量来源会在对应小节注明。
注册基础样式
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);
if (!saved) {
// 检查初始化、样式 ID 和样式对象。
}val style = BasicCustomPushNotification()
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)服务端对应设置 AndroidNotificationBarType=1。这里的 1 是应用自己注册的样式 ID,不是 BasicCustomPushNotification.NOTIFICATION_TYPE_* 类型值。
BasicCustomPushNotification 构造函数
public BasicCustomPushNotification();
public BasicCustomPushNotification(int drawable, int flags, int remindType);
|
参数 |
类型 |
是否必填 |
说明 |
|
drawable |
int |
是 |
状态栏小图标的 drawable 资源 ID,例如 R.drawable.notification_small。 |
|
flags |
int |
是 |
Android Notification 标志位,可按位组合,例如 Notification.FLAG_AUTO_CANCEL。 |
|
remindType |
int |
是 |
REMIND_TYPE_SILENT、REMIND_TYPE_VIBRATE、REMIND_TYPE_SOUND 或 REMIND_TYPE_VIBRATE_AND_SOUND。 |
无参数构造函数默认:状态栏图标为 android.R.drawable.stat_notify_chat;提醒方式为声音加振动;flags 为 Notification.FLAG_AUTO_CANCEL;前台显示通知为 true;服务端提醒方式优先为 false。
|
常量 |
值和含义 |
|
REMIND_TYPE_SILENT |
0,静默。 |
|
REMIND_TYPE_VIBRATE |
1,振动。 |
|
REMIND_TYPE_SOUND |
2,声音。 |
|
REMIND_TYPE_VIBRATE_AND_SOUND |
3,声音加振动。 |
Android 8.0 及以上的通知声音、振动等效果由 NotificationChannel 配置和用户的系统通知设置共同决定。
BasicCustomPushNotification():创建默认基础样式
创建采用默认配置的基础样式对象,不自动注册样式。
接口定义
public BasicCustomPushNotification();
参数说明:无。
返回值:构造函数创建 BasicCustomPushNotification 对象;不是异步接口,无回调。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();val style = BasicCustomPushNotification()BasicCustomPushNotification(int, int, int):指定初始配置
在构造时设置状态栏图标、通知标志和提醒方式,其他配置保持默认值。
接口定义
public BasicCustomPushNotification(int drawable, int flags, int remindType);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
drawable |
int |
是 |
状态栏小图标的 drawable 资源 ID,例如 R.drawable.notification_small。 |
|
flags |
int |
是 |
Android Notification 标志位,可按位组合,例如 Notification.FLAG_AUTO_CANCEL。 |
|
remindType |
int |
是 |
REMIND_TYPE_SILENT、REMIND_TYPE_VIBRATE、REMIND_TYPE_SOUND 或 REMIND_TYPE_VIBRATE_AND_SOUND。 |
返回值:构造函数创建 BasicCustomPushNotification 对象;需另行注册后供通知使用。
代码示例
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 方法
|
接口定义 |
参数/返回值和语义 |
|
int getStatusBarDrawable() |
返回状态栏图标资源 ID。 |
|
void setStatusBarDrawable(int statusBarDrawable) |
设置状态栏图标,参数为 drawable 资源 ID。 |
|
int getRemindType() |
返回 REMIND_TYPE_* 提醒方式。 |
|
void setRemindType(int remindType) |
设置四种合法提醒方式之一;非法值不会替换当前配置。 |
|
int getNotificationFlags() |
返回 Android Notification 标志位。 |
|
void setNotificationFlags(int notificationFlags) |
设置 Android Notification 标志位。 |
|
boolean isServerOptionFirst() |
返回是否优先采用服务端提醒方式,默认 false。 |
|
void setServerOptionFirst(boolean first) |
当服务端提醒方式与客户端样式冲突时,true 使用服务端设定,false 使用客户端设定。 |
|
boolean isBuildWhenAppInForeground() |
返回应用在前台时是否创建通知,默认 true。 |
|
void setBuildWhenAppInForeground(boolean enabled) |
false 表示应用前台时不创建该样式通知;后台仍按默认显示流程处理。 |
|
int getNotificationType() |
返回样式类型:DEFAULT=1、CUSTOM_BASIC=2、CUSTOM_ADVANCED=3;不是注册样式 ID。 |
|
String toString() |
返回样式配置的描述字符串,用于调试。 |
4.x 前台不显示通知时,数据继续通过 onNotification 回调交给应用,不再实现 onNotificationReceivedInApp。如果应用重写了 showNotificationNow,请保留所需的默认展示判断。
getStatusBarDrawable:获取状态栏小图标
获取状态栏小图标,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public int getStatusBarDrawable();
参数说明:无。
返回值:返回当前样式的 drawable 资源 ID。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
int drawable = style.getStatusBarDrawable();val style = BasicCustomPushNotification()
val drawable = style.getStatusBarDrawable()setStatusBarDrawable:设置状态栏小图标
设置状态栏小图标,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public void setStatusBarDrawable(int statusBarDrawable);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
statusBarDrawable |
int |
是 |
状态栏小图标的 drawable 资源 ID,不是通知布局控件 ID。 |
返回值:无(void);仅更新当前样式对象。
代码示例
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:获取提醒方式
获取提醒方式,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public int getRemindType();
参数说明:无。
返回值:返回当前 REMIND_TYPE_* 常量,取值为 0、1、2 或 3。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
int remindType = style.getRemindType();val style = BasicCustomPushNotification()
val remindType = style.getRemindType()setRemindType:设置提醒方式
设置提醒方式,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public void setRemindType(int remindType);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
remindType |
int |
是 |
四种 REMIND_TYPE_* 常量之一。非法值不会替换已有配置。 |
返回值:无(void);Android 8.0 及以上的实际声音和振动还受 NotificationChannel 配置影响。
代码示例
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:获取通知标志
获取通知标志,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public int getNotificationFlags();
参数说明:无。
返回值:返回当前 Android Notification flags。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
int flags = style.getNotificationFlags();val style = BasicCustomPushNotification()
val flags = style.getNotificationFlags()setNotificationFlags:设置通知标志
设置通知标志,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public void setNotificationFlags(int notificationFlags);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
notificationFlags |
int |
是 |
Android Notification 标志位,例如 Notification.FLAG_AUTO_CANCEL;多项可使用按位或组合。 |
返回值:无(void);仅更新当前样式对象。
代码示例
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:获取提醒方式的优先来源
获取提醒方式的优先来源,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public boolean isServerOptionFirst();
参数说明:无。
返回值:true 表示优先使用服务端提醒方式;false 表示优先使用客户端样式,默认 false。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean serverFirst = style.isServerOptionFirst();val style = BasicCustomPushNotification()
val serverFirst = style.isServerOptionFirst()setServerOptionFirst:设置提醒方式的优先来源
设置提醒方式的优先来源,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public void setServerOptionFirst(boolean first);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
first |
boolean |
是 |
true:提醒方式冲突时采用服务端设置;false:采用客户端样式设置。默认 false。 |
返回值:无(void);该选项用于设置提醒方式的来源。
代码示例
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:获取前台展示设置
获取前台展示设置,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public boolean isBuildWhenAppInForeground();
参数说明:无。
返回值:true 表示前台允许创建该样式通知,false 表示前台不创建;默认 true。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean enabled = style.isBuildWhenAppInForeground();val style = BasicCustomPushNotification()
val enabled = style.isBuildWhenAppInForeground()setBuildWhenAppInForeground:设置前台展示行为
设置前台展示行为,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public void setBuildWhenAppInForeground(boolean enabled);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
enabled |
boolean |
是 |
false:应用前台时不创建该样式通知;true:允许创建。后台仍按默认展示判断处理。 |
返回值:无(void);应用仍可通过 onNotification 获取通知数据。覆盖 showNotificationNow 时,应保留需要的默认规则。
代码示例
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:获取样式类型
获取样式类型,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public int getNotificationType();
参数说明:无。
返回值:基础样式返回 NOTIFICATION_TYPE_CUSTOM_BASIC(2),高级样式返回 NOTIFICATION_TYPE_CUSTOM_ADVANCED(3);此值不是注册样式 ID。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
int notificationType = style.getNotificationType();val style = BasicCustomPushNotification()
val notificationType = style.getNotificationType()toString:获取样式描述
获取样式描述,作用于 BasicCustomPushNotification 或继承它的高级样式对象。
接口定义
public String toString();
参数说明:无。
返回值:返回配置描述字符串,供调试使用。样式持久化请通过 setCustomNotification 完成。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
String description = style.toString();val style = BasicCustomPushNotification()
val description = style.toString()AdvancedCustomPushNotification 构造函数
AdvancedCustomPushNotification 继承 BasicCustomPushNotification 的上述配置方法,用于自定义通知内容布局,没有无参数构造函数。
public AdvancedCustomPushNotification(int view, int iconViewId,
int titleViewId, int contentViewId);
|
参数 |
类型 |
是否必填 |
说明 |
|
view |
int |
是 |
符合 RemoteViews 要求的 R.layout 布局资源 ID。 |
|
iconViewId |
int |
是 |
布局内 ImageView 控件的 R.id;不是图片 drawable ID。 |
|
titleViewId |
int |
是 |
布局内显示标题的 TextView 控件 R.id。 |
|
contentViewId |
int |
是 |
布局内显示正文的 TextView 控件 R.id。 |
创建 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>
返回值:构造函数创建 AdvancedCustomPushNotification 对象;样式需通过 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
)setIcon 接收图片资源 ID(R.drawable),不是 ImageView 的 R.id。系统版本可能限制自定义布局的展示面积和样式,应在目标系统上验证。
AdvancedCustomPushNotification 方法
|
接口定义 |
参数/返回值 |
|
int getNotificationView() |
返回布局资源 ID。 |
|
int getIconView() |
返回图标控件 ID。 |
|
int getTitleView() |
返回标题控件 ID。 |
|
int getContentView() |
返回正文控件 ID。 |
|
int getIcon() |
返回图标图片资源 ID。 |
|
void setIcon(int icon) |
设置显示在 iconViewId 对应控件中的图片,参数为 drawable 资源 ID。 |
|
String toString() |
返回样式描述信息。 |
提醒方式、状态栏图标、flags、服务端优先和前台展示配置均继承自 BasicCustomPushNotification。
getNotificationView:获取布局资源
获取布局资源,作用于 AdvancedCustomPushNotification 对象。
接口定义
public int getNotificationView();
参数说明:无。
返回值:返回构造函数传入的布局资源 ID。
代码示例
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:获取图标控件
获取图标控件,作用于 AdvancedCustomPushNotification 对象。
接口定义
public int getIconView();
参数说明:无。
返回值:返回构造函数传入的 ImageView 控件 ID,不是图片资源 ID。
代码示例
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:获取标题控件
获取标题控件,作用于 AdvancedCustomPushNotification 对象。
接口定义
public int getTitleView();
参数说明:无。
返回值:返回构造函数传入的标题 TextView 控件 ID。
代码示例
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:获取正文控件
获取正文控件,作用于 AdvancedCustomPushNotification 对象。
接口定义
public int getContentView();
参数说明:无。
返回值:返回构造函数传入的正文 TextView 控件 ID。
代码示例
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:获取图标图片
获取图标图片,作用于 AdvancedCustomPushNotification 对象。
接口定义
public int getIcon();
参数说明:无。
返回值:返回 setIcon 设置的 drawable 资源 ID;未设置时为 0。
代码示例
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:设置图标图片
设置图标图片,作用于 AdvancedCustomPushNotification 对象。
接口定义
public void setIcon(int icon);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
icon |
int |
是 |
显示在 iconViewId 对应 ImageView 中的 drawable 资源 ID,例如 R.drawable.notification_large。 |
返回值:无(void);这不是状态栏小图标配置。
代码示例
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:获取高级样式描述
获取高级样式描述,作用于 AdvancedCustomPushNotification 对象。
接口定义
public String toString();
参数说明:无。
返回值:返回基础配置和布局配置的描述字符串,供调试使用。样式持久化请通过 setCustomNotification 完成。
代码示例
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);
|
参数/返回值 |
说明 |
|
customNotificationId |
大于 0 的整数;与服务端 AndroidNotificationBarType 使用的 ID 一致。 |
|
notification |
非空 BasicCustomPushNotification 或其子类 AdvancedCustomPushNotification 对象。 |
|
boolean 返回值 |
true 表示保存成功;false 表示失败,应检查 SDK 是否初始化、ID/对象是否合法。 |
先调用 PushServiceFactory.init,再注册样式。SDK 会保存样式配置并在需要时加载;样式发生变化时,再使用同一 ID 注册更新后的配置。
getInstance:获取样式管理器
获取 CustomNotificationBuilder 单例,使用该对象注册样式。
接口定义
public static CustomNotificationBuilder getInstance();
参数说明:无。
返回值:CustomNotificationBuilder 单例对象。
代码示例
CustomNotificationBuilder builder = CustomNotificationBuilder.getInstance();val builder = CustomNotificationBuilder.getInstance()setCustomNotification:注册或更新样式
把样式对象保存到指定 ID;同一 ID 再次注册会覆盖之前配置。
接口定义
public boolean setCustomNotification(int customNotificationId, BasicCustomPushNotification notification);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
customNotificationId |
int |
是 |
大于 0 的样式 ID,与服务端 AndroidNotificationBarType 一致。 |
|
notification |
BasicCustomPushNotification |
是 |
非 null 的基础样式或 AdvancedCustomPushNotification 对象。 |
返回值:boolean。true 表示注册成功;false 表示未初始化、参数非法或序列化保存发生错误。
代码示例
BasicCustomPushNotification style = new BasicCustomPushNotification();
boolean saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style);
if (!saved) {
// 检查 SDK 初始化、样式 ID 和样式对象。
}val style = BasicCustomPushNotification()
val saved = CustomNotificationBuilder.getInstance().setCustomNotification(1, style)
if (!saved) {
// 检查 SDK 初始化、样式 ID 和样式对象。
}按推送数据调整通知构建
Android SDK 4.x 中,MessageReceiver 和 AliyunMessageIntentService 的业务回调范围如下:
ACCS 自有通道和 FCM:支持消息接收(onMessage)、通知到达(onNotification)、通知点击(onNotificationOpened)、通知清除(onNotificationRemoved)等完整业务回调。
除 FCM 外的其他 Android 厂商通道:仅支持 onNotificationOpened(通知点击),不触发消息接收、通知到达、通知清除等其他回调。
需要在 SDK 创建通知时进一步配置,可在自定义 MessageReceiver 或 AliyunMessageIntentService 中重写 hookNotificationBuild(),返回 NotificationConfigure。三个方法分别对应系统 Builder、兼容 Builder 和构建后的 Notification。
public NotificationConfigure hookNotificationBuild();
public Notification customNotificationUI(Context context, PushData pushData);
customNotificationUI(Context,PushData) 返回非 null 时使用应用返回的通知;返回 null 则继续 SDK 默认样式构建。需要自行构建时,请正确处理系统通知渠道和通知显示要求。
|
PushData 读取方法 |
说明 |
|
getTitle()/getContentText() |
通知标题和正文。 |
|
getImage()/getSound()/getNotifyType() |
图片、声音和提醒方式参数。 |
|
getExtraMap() |
业务扩展参数,使用前判空。 |
|
getNotificationChannel()/getPriority() |
通知渠道 ID 和优先级。 |
|
getCustomNotificationId()/getStyle() |
自定义样式 ID 和服务端样式参数。 |
|
getBigTitle()/getBigBody()/getBigPicture()/getInboxContent() |
大文本、大图或多行通知相关参数。 |
|
getNotifyId()/getMsgId() |
整数通知 ID 和服务端消息 ID,含义不同。 |
以下重写示例放入自定义 MessageReceiver 或 AliyunMessageIntentService;保留该类必需的四个业务回调。NotificationConfigure 的方法写在 hookNotificationBuild 返回的实现中。PushData 由 SDK 传入,下面的读取示例均使用此对象。
hookNotificationBuild:提供通知构建配置
返回 NotificationConfigure 实现,SDK 在默认构建流程的相应阶段调用其中方法。
接口定义
public NotificationConfigure hookNotificationBuild();
参数说明:无。
返回值:NotificationConfigure 对象;返回 null 不添加此扩展配置。customNotificationUI 返回自建通知时,不再经过 SDK 默认通知构建的这些配置阶段。
代码示例
@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) {
// 可按业务需要读取 data 并调整最终 Notification。
}
};
}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:返回应用构建的通知
重写此方法可替换 SDK 默认通知构建结果。返回 Notification 后,由 SDK 统一处理通知展示。
接口定义
public Notification customNotificationUI(Context context, PushData pushData);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
context |
Context |
是 |
SDK 创建通知使用的上下文。 |
|
pushData |
PushData |
否 |
SDK 解析得到的通知数据;缺少有效标题或正文时解析可能返回 null,使用前检查。 |
返回值:Notification。非 null:使用应用返回的通知;null:继续 SDK 默认构建。
此示例在没有通知渠道 ID 时回退到 SDK 默认构建。Android 8.0 及以上必须预先创建所使用的 NotificationChannel,并确认应用已获得系统通知权限。
代码示例
@Override
public Notification customNotificationUI(Context context, PushData pushData) {
if (pushData == null || android.text.TextUtils.isEmpty(pushData.getNotificationChannel())) {
return null;
}
// 对应通知渠道应已由应用创建;示例资源请替换为应用实际资源。
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
// 对应通知渠道应已由应用创建。
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):配置系统 Builder
SDK 使用系统 Builder 构建通知时调用此重载,使用兼容 Builder 时调用对应的另一个重载。
接口定义
void configBuilder(Notification.Builder builder, PushData pushData);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
builder |
Notification.Builder |
是 |
SDK 正在使用的通知构建器,在本回调内修改。 |
|
pushData |
PushData |
否 |
本次通知解析数据;读取可选字段前判空。 |
返回值:无(void)。直接修改传入的 builder,SDK 继续构建通知。
将示例替换到上方 NotificationConfigure 实现的对应重载中,其他两个方法仍需实现。
代码示例
@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):配置兼容 Builder
SDK 使用兼容 Builder 构建通知时调用此重载,使用系统 Builder 时调用对应的另一个重载。
接口定义
void configBuilder(androidx.core.app.NotificationCompat.Builder builder, PushData pushData);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
builder |
androidx.core.app.NotificationCompat.Builder |
是 |
SDK 正在使用的通知构建器,在本回调内修改。 |
|
pushData |
PushData |
否 |
本次通知解析数据;读取可选字段前判空。 |
返回值:无(void)。直接修改传入的 builder,SDK 继续构建通知。
将示例替换到上方 NotificationConfigure 实现的对应重载中,其他两个方法仍需实现。
代码示例
@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:调整构建后的通知
在 SDK 默认构建得到 Notification 后调整该通知对象。
接口定义
void configNotification(Notification notification, PushData pushData);
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
notification |
Notification |
是 |
SDK 已构建的通知对象。 |
|
pushData |
PushData |
否 |
本次通知解析数据;读取可选字段前判空。 |
返回值:无(void)。直接修改传入的 notification,由 SDK 统一展示通知。
将示例替换到上方 NotificationConfigure 实现中,两个 configBuilder 重载仍需实现。
代码示例
@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:获取通知标题
读取 SDK 传入的 PushData 对象中的通知标题。
接口定义
public String getTitle();
参数说明:无。
返回值:返回通知标题。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getTitle();
}val value = pushData?.getTitle()PushData.getContentText:获取通知正文
读取 SDK 传入的 PushData 对象中的通知正文。
接口定义
public String getContentText();
参数说明:无。
返回值:返回通知正文。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getContentText();
}val value = pushData?.getContentText()PushData.getImage:获取通知图片
读取 SDK 传入的 PushData 对象中的通知图片。
接口定义
public String getImage();
参数说明:无。
返回值:返回通知图片字段(如图片 URL),不是 Bitmap;值可能为 null。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getImage();
}val value = pushData?.getImage()PushData.getSound:获取声音字段
读取 SDK 传入的 PushData 对象中的声音字段。
接口定义
public String getSound();
参数说明:无。
返回值:返回下发的声音字段,可能为 null;实际声音由通知配置及系统设置决定。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getSound();
}val value = pushData?.getSound()PushData.getNotifyType:获取提醒方式
读取 SDK 传入的 PushData 对象中的提醒方式。
接口定义
public int getNotifyType();
参数说明:无。
返回值:返回 NOTIFY_TYPE_SILENT(0)、NOTIFY_TYPE_VIBRATE(1)、NOTIFY_TYPE_SOUND(2)或 NOTIFY_TYPE_VIBRATE_SOUND(3)。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
int value = pushData.getNotifyType();
}val value = pushData?.getNotifyType()PushData.getExtraMap:获取扩展参数
读取 SDK 传入的 PushData 对象中的扩展参数。
接口定义
public Map<String, String> getExtraMap();
参数说明:无。
返回值:返回从原始通知 ext JSON 解析出的扩展参数 Map,可能为 null;键名和业务含义由发送方约定。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
Map<String, String> value = pushData.getExtraMap();
}val value = pushData?.getExtraMap()PushData.getNotificationChannel:获取通知渠道
读取 SDK 传入的 PushData 对象中的通知渠道。
接口定义
public String getNotificationChannel();
参数说明:无。
返回值:返回通知渠道 ID,可能为 null;该方法不会创建 NotificationChannel。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getNotificationChannel();
}val value = pushData?.getNotificationChannel()PushData.getPriority:获取通知优先级
读取 SDK 传入的 PushData 对象中的通知优先级。
接口定义
public int getPriority();
参数说明:无。
返回值:返回通知 priority(通常为 -2 至 2);未指定时为默认优先级 0,Android 8.0 及以上的提示效果还受渠道重要性影响。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
int value = pushData.getPriority();
}val value = pushData?.getPriority()PushData.getCustomNotificationId:获取自定义样式 ID
读取 SDK 传入的 PushData 对象中的自定义样式 ID。
接口定义
public int getCustomNotificationId();
参数说明:无。
返回值:返回服务端选择的客户端注册样式 ID;未指定时为 NO_CUSTOM_NOTIFICATION。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
int value = pushData.getCustomNotificationId();
}val value = pushData?.getCustomNotificationId()PushData.getStyle:获取服务端样式字段
读取 SDK 传入的 PushData 对象中的服务端样式字段。
接口定义
public String getStyle();
参数说明:无。
返回值:返回服务端 style 字段,可能为 null;此字段与注册样式 ID 不同。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getStyle();
}val value = pushData?.getStyle()PushData.getBigTitle:获取大样式标题
读取 SDK 传入的 PushData 对象中的大样式标题。
接口定义
public String getBigTitle();
参数说明:无。
返回值:返回大文本或大图通知的标题字段,可能为 null。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getBigTitle();
}val value = pushData?.getBigTitle()PushData.getBigBody:获取大文本正文
读取 SDK 传入的 PushData 对象中的大文本正文。
接口定义
public String getBigBody();
参数说明:无。
返回值:返回大文本通知的正文字段,可能为 null。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getBigBody();
}val value = pushData?.getBigBody()PushData.getBigPicture:获取大图字段
读取 SDK 传入的 PushData 对象中的大图字段。
接口定义
public String getBigPicture();
参数说明:无。
返回值:返回大图通知的图片字段,可能为 null;不是已下载的 Bitmap。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getBigPicture();
}val value = pushData?.getBigPicture()PushData.getInboxContent:获取多行通知内容
读取 SDK 传入的 PushData 对象中的多行通知内容。
接口定义
public String getInboxContent();
参数说明:无。
返回值:返回服务端多行通知内容字段,可能为 null;该方法不会转换为列表控件。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getInboxContent();
}val value = pushData?.getInboxContent()PushData.getNotifyId:获取整数通知 ID
读取 SDK 传入的 PushData 对象中的整数通知 ID。
接口定义
public int getNotifyId();
参数说明:无。
返回值:返回 Android 通知使用的整数 ID;不是服务端消息标识,缺失或解析失败时为 0。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
int value = pushData.getNotifyId();
}val value = pushData?.getNotifyId()PushData.getMsgId:获取服务端消息 ID
读取 SDK 传入的 PushData 对象中的服务端消息 ID。
接口定义
public String getMsgId();
参数说明:无。
返回值:返回服务端消息标识,可能为 null;不可当作整数通知 ID 使用。
示例放在持有 pushData 参数的通知构建回调中。此读取方法本身不更改通知样式。
代码示例
if (pushData != null) {
String value = pushData.getMsgId();
}val value = pushData?.getMsgId()验证
分别测试默认样式、已注册样式、未注册 ID、前台不展示、ACCS 自有通道点击和 Android 8.0 及以上的通知渠道,并检查系统通知权限。
相关文档
接入流程请参见Android SDK接入(4.x)。
使用 3.x 的应用请参见自定义通知样式接口(3.x)。