自定义通知样式相关接口

前言

移动推送Android SDK支持用户自定义通知样式,支持两种自定义场景。

  • BasicCustomPushNotification:使用系统自带的默认通知样式,在此基础上设置样式,包括提醒方式、状态栏图标以及当推送消息到达时应用处于前台的情况下是否创建该通知等。

  • AdvancedCustomPushNotification:自定义通知样式布局文件,在此基础上设置样式,包括提醒方式、状态栏图标以及当推送消息到达时应用处于前台的情况下是否创建该通知等。

使用自定义通知样式包含两个步骤:

第一步:客户端设置通知样式

第二步:后端推送消息时指定使用自定义样式

重要

SDK V2.3.3及以上版本支持自定义样式通知。

第一步:客户端设置通知样式

根据您自定义样式的场景,选择使用BasicCustomPushNotification或者AdvancedCustomPushNotification

每个样式都需要对应一个特定的整数类型ID,如果多个样式设置为同一个ID,则最后设置的样式有效。

这个ID在后端推送时需要使用到,SDK在创建通知时,如果没有找到对应ID的样式则会创建默认样式的通知。

样式只需设置一次,SDK会保存这个设置,在需要使用时加载对应样式。具体使用例子请参考Demo

BasicCustomPushNotification代码示例

val notification = BasicCustomPushNotification()
notification.remindType = BasicCustomPushNotification.REMIND_TYPE_SOUND
notification.statusBarDrawable = R.drawable.logo_yuanjiao_120
val res = CustomNotificationBuilder.getInstance().setCustomNotification(1, notification)
BasicCustomPushNotification notification = new BasicCustomPushNotification();
notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SOUND);
notification.setStatusBarDrawable(R.drawable.logo_yuanjiao_120);
boolean res = CustomNotificationBuilder.getInstance().setCustomNotification(1, notification);

AdvancedCustomPushNotification代码示例

val notification = AdvancedCustomPushNotification(
    R.layout.notitfication_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.notitfication_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);

第二步:后端推送消息时指定使用自定义样式

客户端设置完成后,服务端在推送通知时需要利用OpenAPI指定对应的自定义样ID。

代码示例

final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
final String date = dateFormat.format(new Date());
PushRequest pushRequest = new PushRequest();
// 推送目标
pushRequest.setAppKey(appKey);
pushRequest.setTarget("device"); //推送目标: device:推送给设备; account:推送给指定账号,tag:推送给自定义标签; all: 推送给全部
pushRequest.setTargetValue("deviceId"); 
// 推送配置
pushRequest.setType(1); // 0:表示消息(默认为0), 1:表示通知
pushRequest.setTitle(date); // 消息的标题
pushRequest.setBody("PushRequest body"); // 消息的内容
pushRequest.setSummary("PushRequest summary"); // 通知的摘要
pushRequest.setAndroidNotificationBarType(2);//设置的通知样式ID,通知栏自定义样式范围0-100
// 推送配置: Android
pushRequest.setAndroidOpenType("1"); // 点击通知后动作,1:打开应用 2: 打开应用Activity 3:打开 url
pushRequest.setAndroidExtParameters("{\"_NOTIFICATION_BAR_STYLE_\":\"2\"}");

BasicCustomPushNotification

BasicCustomPushNotification支持设置提醒方式、状态栏图标以及当推送消息到达时应用处于前台的情况下是否创建该通知等。

BasicCustomPushNotification

默认构造函数,所有配置采用默认设置:通知方式采用震动+通知;NotificationFlag采用Notification.FLAG_AUTO_CANCEL,状态栏图标用的是android.R.drawable.stat_notify_chat。

接口定义

public BasicCustomPushNotification()

所属类

BasicCustomPushNotification

BasicCustomPushNotification

构造函数。

接口定义

public BasicCustomPushNotification(int drawable, int flags, int remindType)

所属类

BasicCustomPushNotification

参数说明

参数

类型

是否必填

说明

drawable

int

状态栏图标

flags

int

NotificationFlags,支持系统Notification下的Flag参数

remindType

int

提醒方式类型,支持以下选择:

  • BasicCustomPushNotification.REMIND_TYPE_SILENT:静默;

  • BasicCustomPushNotification.REMIND_TYPE_VIBRATE:震动;

  • BasicCustomPushNotification.REMIND_TYPE_SOUND:声音;

  • BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND:声音+震动

代码示例

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

设置自定义通知的状态栏图标。

接口定义

public void setStatusBarDrawable(int statusBarDrawable)

所属类

BasicCustomPushNotification

参数说明

参数

类型

是否必填

说明

statusBarDrawable

int

状态栏图标资源ID

代码示例

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

设置自定义通知的提醒方式。

接口定义

public void setRemindType(int remindType)

所属类

BasicCustomPushNotification

参数说明

参数

类型

是否必填

说明

remindType

int

提醒方式类型,支持以下选择:

  • BasicCustomPushNotification.REMIND_TYPE_SILENT:静默;

  • BasicCustomPushNotification.REMIND_TYPE_VIBRATE:震动;

  • BasicCustomPushNotification.REMIND_TYPE_SOUND:声音;

  • BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND:声音+震动

代码示例

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

设置自定义通知的flags参数。

接口定义

public void setNotificationFlags(int notificationFlags)

所属类

BasicCustomPushNotification

参数说明

参数

类型

是否必填

说明

notificationFlags

int

支持系统自带的Notification Flag参数

代码示例

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

设置自定义通知的serverOptionFirst参数。

说明

利用OpenAPI或者阿里云推送控制台推送消息都可以设置提醒方式,当后端设置的提醒方式和自定义样式提醒方式冲突时,SDK根据serverOptionFirst参数来判断提醒方式策略。如果该参数为true,则采用后端设定的提醒方式;如果该参数为false,则采用自定义样式指定的提醒方式。默认为false。

接口定义

public void setServerOptionFirst(boolean serverOptionFirst)

所属类

BasicCustomPushNotification

参数说明

参数

类型

是否必填

说明

serverOptionFirst

boolean

是否服务器配置优先。

  • true:采用后端设定的提醒方式

  • false:采用自定义样式指定的提醒方式

默认值为false。

代码示例

val customPushNotification = BasicCustomPushNotification()
customPushNotification.isServerOptionFirst = true
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)
BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setServerOptionFirst(true);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);

setBuildWhenAppInForeground

设置当推送到达时应用处在前台的情况下是否创建自定义通知。默认是创建通知。

接口定义

public void setBuildWhenAppInForeground(boolean buildWhenAppInForeground)

所属类

BasicCustomPushNotification

参数说明

参数

类型

是否必填

说明

buildWhenAppInForeground

boolean

是否创建通知

代码示例

val customPushNotification = BasicCustomPushNotification()
customPushNotification.isBuildWhenAppInForeground = true
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification)
BasicCustomPushNotification customPushNotification = new BasicCustomPushNotification();
customPushNotification.setBuildWhenAppInForeground(true);
CustomNotificationBuilder.getInstance().setCustomNotification(1, customPushNotification);

AdvancedCustomPushNotification

AdvancedCustomPushNotificationBasicCustomPushNotification的子类,继承了BasicCustomPushNotification的所有方法,同时也提供更丰富的配置。

AdvancedCustomPushNotification

构造函数,AdvancedCustomPushNotification没有默认构造函数。

接口定义

public AdvancedCustomPushNotification( int view, int iconViewId, int titleViewId, int contentViewId)

所属类

AdvancedCustomPushNotification

参数说明

参数

类型

是否必填

说明

view

int

自定义通知布局文件ID。

说明

Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局。

iconViewId

int

自定义布局文件中icon的viewId。

titleViewId

int

自定义布局文件中title的viewId。

contentViewId

int

自定义布局文件中显示通知正文的viewId。

代码示例

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_cus_notif, R.id.m_icon, R.id.m_title, R.id.m_text);
CustomNotificationBuilder.getInstance().setCustomNotification(2, customPushNotification);

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

设置通知栏中显示的图标,该图标显示在iconViewId所指定的控件中。

接口定义

public void setIcon(int icon)

所属类

AdvancedCustomPushNotification

参数说明

参数

类型

是否必填

说明

icon

int

icon图标资源ID。

代码示例

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);