204
微信
SDK API介绍__Android SDK手册_App SDK 手册_移动推送-阿里云
Android API
Android SDK最新版本v2.3.6。
API索引:
- 1. CloudPushSDK接口
- 2. MessageReceiver相关接口
- 3. 自定义样式通知相关接口
1. CloudPushService接口
以下接口调用时,如有回调,均为异步执行,且回调不能为空。
1.1基本设置
SDK注册
- 初始化推送SDK,关联到云通道。
参数
- context 应用上下文(需要
ApplicationContext
) - callback 回调
void register(Context context, CommonCallback callback);
启动信息统计
- 统计App启动信息。
void onAppStart();
获取设备标识
- 获取设备唯一标识。
返回
- 设备唯一标识。
String getDeviceId();
设置日志等级
- 需要在通道初始化之前设置;
- 默认日志等级为
CloudPushService.ERROR
;
参数
- logLevel 支持设置:
CloudPushService.ERROR
|CloudPushService.INFO
|CloudPushService.DEBUG
|CloudPushService.OFF
(关闭Log)
void setLogLevel(int logLevel);
动态设置appKey(V2.3.6
及以上版本支持)
- 动态设置appKey无需在manifest配置appKey
- 务必在调用
register
注册接口之前调用该接口,否则动态设置失效
参数
- appKey 需要设置的appKey
void setAppKey(String appKey);
动态设置appSecret(V2.3.6
及以上版本支持)
- 动态设置appSecret无需在manifest配置appSecret
- 务必在调用
register
注册接口之前调用该接口,否则动态设置失效
参数
- appSecret 需要设置的appSecret
void setAppSecret(String appSecret);
1.2 账号API
绑定账号
- 将应用内账号和推送通道相关联,可以实现按账号的定点消息推送;
- 设备只能绑定一个账号,更换绑定账号时无需进行解绑,重新调用绑定账号接口即可生效;
- 若业务场景需要先解绑后绑定,在解绑账号成功回调中进行绑定绑定操作,以此保证执行的顺序性;
- 账户名设置支持
32
字节。
参数
- account 绑定账号名
- callback 回调
void bindAccount(String account, CommonCallback callback);
解绑账号
- 将应用内账号和推送通道取消关联。
参数
- callback 回调
void unbindAccount(CommonCallback callback);
1.3 标签API
绑定标签
- 绑定标签到指定目标;
- 支持向设备、账号和别名绑定标签,绑定类型由参数
target
指定; - 绑定标签后,第二天服务端可按该标签推送,即
(T + 1)
天生效; - App最多支持绑定
128
个标签,【请谨慎使用,避免标签绑定达到上限】,单个标签最大支持40
字节。
参数
- target 目标类型,1:本设备; 2:本设备绑定账号; 3:别名
- target(
V2.3.5及以上版本
) 目标类型,CloudPushService.DEVICE_TARGET:本设备; CloudPushService.ACCOUNT_TARGET:本账号; CloudPushService.ALIAS_TARGET:别名 - tags 标签(数组输入)
- alias 别名(仅当target = 3时生效)
- callback 回调
void bindTag(int target, String[] tags, String alias, CommonCallback callback);
解绑标签
- 解绑指定目标标签;
- 支持解绑设备、账号和别名标签,解绑类型由参数
target
指定; - 解绑标签后,当天服务端可继续按该标签推送,解绑生效时间在第二天,即
(T + 1)
天生效; 解绑标签
不等同于删除标签
,目前不支持标签的删除。
参数
- target 目标类型,1:本设备; 2:本设备绑定账号; 3:别名。
- target(
V2.3.5及以上版本
) 目标类型,CloudPushService.DEVICE_TARGET:本设备; CloudPushService.ACCOUNT_TARGET:本账号; CloudPushService.ALIAS_TARGET:别名 - tags 标签(数组输入)
- alias 别名(仅当target = 3时生效)
- callback 回调
void unbindTag(int target, String[] tags, String alias, CommonCallback callback);
查询标签
- 查询目标绑定标签,当前仅支持查询设备标签;
- 查询结果可从回调onSuccess(response)的response获取;
- 标签绑定成功后即可查询。
参数
- target 目标类型,1: 本设备
- target(
V2.3.5及以上版本
) 目标类型,CloudPushService.DEVICE_TARGET:本设备; - callback 回调
void listTags(int target, CommonCallback callback);
1.4 别名API
添加别名
- 设备添加别名;
- 单个设备最多添加
128
个别名,且同一别名最多添加到128
个设备; - 别名支持
128
字节。
参数
- alias 别名
- callback 回调
void addAlias(String alias, CommonCallback callback);
删除别名
- 删除设备别名;
- 支持删除指定别名和删除全部别名(alias = null || alias.length = 0)。
参数
- alias 别名(
alias = null or alias.length = 0
时,删除设备全部别名) - callback 回调
void removeAlias(String alias, CommonCallback callback);
查询别名
- 查询设备别名;
- 查询结果可从回调onSuccess(response)的response中获取
参数
- callback 回调
void listAliases(CommonCallback callback);
1.5 通知设置API
设置通知声音
- 设置推送通知声音文件路径;
- 若不调用本接口,默认获取资源id为
R.raw.alicloud_notification_sound
的资源文件; - 若没有获取到指定声音文件,取设备设置的消息声音。
参数
- filePath 通知声音文件路径
void setNotificationSoundFilePath(String filePath);
设置通知栏图标
- 设置推送通知栏图标资源Bitmap。
- 若不调用本接口,默认获取id为R.drawable.alicloud_notification_largeIcon的资源文件;
- 若没有获取到指定图标文件,取App启动图标。
参数
- icon 图标资源Bitmap
void setNotificationLargeIcon(Bitmap icon);
设置状态栏图标
- 设置推送状态栏图标资源Id;
- 若不调用本接口,默认获取id为R.drawable.alicloud_notification_smallIcon的资源文件;
- 若没有获取到指定资源文件Id,取App启动图标。
参数
- iconId 图标资源Id
void setNotificationSmallIcon(int iconId);
设置免打扰时段
- 设置免打扰时间段,过滤所有通知与消息;
- 免打扰时段仅支持设置一次,多次调用以最后一次调用设置时段为准;
- 设置免打扰时段为00:00 - 00:00,可取消免打扰功能;(
V2.3.4及以下功能支持该用法,V2.3.5及以上版本使用关闭免打扰功能接口
) - 全天免打扰可以设置为”0:0-23:59”
- 免打扰时段设置对小米辅助弹窗通知无效。
参数
- startHour 免打扰的起始时间(小时),24小时制,取值范围:0-23
- startMinute 免打扰起始时间(分钟),取值范围:0-59
- endHour 免打扰的结束时间(小时),24小时制,取值范围:0-23
- endMinute 免打扰结束时间(分钟),取值范围:0-59
void setDoNotDisturb(int startHour, int startMinute, int endHour, int endMinute, CommonCallback callback);
关闭免打扰功能(V2.3.5
及以上版本支持)
- 关闭后,先前设置的免打扰时段失效
- 免打扰功能默认是关闭的
- 没有对应的开发免打扰功能接口,调用设置
免打扰功能时段
功能后自动打开免打扰功能
void closeDoNotDisturbMode();
2. MessageReceiver
- 通过继承
MessageReciever
,可以拦截通知,接收消息,获取推送中的扩展字段。或者在通知打开或删除的时候,切入进行后续处理。
使用方法:
- 继承
com.alibaba.sdk.android.push.MessageReceiver
; - 在Manifest中找到原来MessageReceiver的配置,将上边的class替换成你自己的receiver[不要配置多个]。
<!--消息接收监听器-->
<receiver android:name="com.alibaba.sdk.android.push.MessageReceiver <-- 把这里替换成你自己的receiver">
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED"/>
</intent-filter>
... ...
</receiver>
消息接收回调
- 用于接收服务端推送的消息。
- 消息不会弹窗,而是回调该方法。
参数
- context 上下文环境
- message
CPushMessage
类型,可以获取消息Id、消息标题和内容。
void onMessage(Context context, CPushMessage message);
通知接收回调
- 客户端接收到通知后,回调该方法。
- 可获取到并处理通知相关的参数。
参数
- context 上下文环境
- title 通知标题
- summary 通知内容
- extraMap 通知额外参数,包括部分系统自带参数:
_ALIYUN_NOTIFICATION_ID_(V2.3.5及以上)
:创建通知对应id_ALIYUN_NOTIFICATION_PRIORITY_(V2.3.5及以上)
:创建通知对应id。默认不带,需要通过OpenApi设置
void onNotification(Context context, String title, String summary, Map<String, String> extraMap)
通知打开回调
- 打开通知时会回调该方法,通知打开上报由SDK自动完成。
参数
- context 上下文环境
- title 通知标题
- summary 通知内容
- extraMap 通知额外参数,包括部分系统自带参数:
_ALIYUN_NOTIFICATION_ID_(V2.3.5及以上)
:创建通知对应id_ALIYUN_NOTIFICATION_PRIORITY_(V2.3.5及以上)
:创建通知对应id。默认不带,需要通过OpenApi设置
void onNotificationOpened(Context context, String title, String summary, String extraMap);
无跳转逻辑通知打开回调
- 打开无跳转逻辑(open=4)通知时回调该方法(
v2.3.2及以上版本支持
),通知打开上报由SDK自动完成。
参数
- context 上下文环境
- title 通知标题
- summary 通知内容
- extraMap 通知额外参数,包括部分系统自带参数:
_ALIYUN_NOTIFICATION_ID_(V2.3.5及以上)
:创建通知对应id_ALIYUN_NOTIFICATION_PRIORITY_(V2.3.5及以上)
:创建通知对应id。默认不带,需要通过OpenApi设置
void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap);
通知删除回调
- 删除通知时回调该方法,通知删除上报由SDK自动完成。
参数
- context 上下文环境
- messageId 删除通知的Id
void onNotificationRemoved(Context context, String messageId);
通知在应用内到达回调
- 当用户创建自定义通知样式,并且设置推送应用内到达不创建通知弹窗时调用该回调,且此时不调用
onNotification
回调(v2.3.3及以上版本支持
)
参数
- context 上下文环境
- title 通知标题
- summary 通知内容
- extraMap 通知额外参数
- openType 原本通知打开方式,1:打开APP;2:打开activity;3:打开URL;4:无跳转逻辑
- openActivity 所要打开的activity的名称,仅当openType=2时有效,其余情况为null
- openUrl 所要打开的URL,仅当openType=3时有效,其余情况为null
void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl);
3. 自定义样式通知(V2.3.3及以上版本开始支持
)
Android Push SDK支持用户自定义通知样式,用户可以设定自己的通知样式,涉及的内容包括通知的提醒方式(声音、震动、静默),通知在状态栏的显示图标,推送消息应用内到达时是否创建通知以及自定义通知布局文件等。自定义样式通知的设置包括两部分:
3.1 客户端设置通知样式
- 用户利用SDK提供的自定义通知样式接口创建自定义样式通知。SDK中有两个自定义样式通知类:1)
BasicCustomPushNotification
;2)AdvancedCustomPushNotification
。其中BasicCustomPushNotification
用户设置基础样式,包括提醒方式、状态栏图标以及当推送消息到达时应用正处于前台情况下是否创建该通知等。AdvancedCustomPushNotification
是BasicCustomPushNotification
的子类,继承了BasicCustomPushNotification
的所有方法,同时还可以设置通知样式布局文件 - 每个样式都需要对应一个特定的整数类型id,如果多个样式设置为同一个id,则最后设置的样式有效。如果SDK没有找到对应id的样式则会创建默认样式的通知
- 样式只需设置一次,SDK会记住这个设置,在需要使用时加载对应样式
- 具体使用例子请参考Demo
3.2 后端推送消息时添加自定义样式id
- 用户利用OpenApi推送消息时设定特定样式的id
- 服务端不能设置样式,只能指定需要展现的样式id
- 指定id的样式必须在客户端已经进行设置,否则SDK会创建默认样式的通知
3.3 Example
Example-BasicCustomPushNotification
BasicCustomPushNotification notification = new BasicCustomPushNotification();
notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SOUND);
notification.setStatusBarDrawable(R.drawable.logo_yuanjiao_120);
boolean res = CustomNotificationBuilder.getInstance().setCustomNotification(1, notification);
Example-AdvancedCustomPushNotification
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().setCustomNotificaon(2, notification);
Example-OpenApi
客户端设置完成后,服务端在推送通知时需要利用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"); // 通知的摘要
// 推送配置: Android
pushRequest.setAndroidOpenType("1"); // 点击通知后动作,1:打开应用 2: 打开应用Activity 3:打开 url
pushRequest.setAndroidExtParameters("{"_NOTIFICATION_BAR_STYLE_":"2"}");
3.4 BasicCustomPushNotification API
默认构造函数
- BasicCustomPushNotification的默认构造函数,所有配置采用默认设置:通知方式采用震动+通知;NotificationFlag采用Notification.FLAG_AUTO_CANCEL,状态栏图标用的是android.R.drawable.stat_notify_chat。
public BasicCustomPushNotification();
构造函数
参数
- drawable 状态栏图标
- flags NotificationFlags,支持系统Notification下的Flag参数
- remindType 提醒类型,有BasicCustomPushNotification.REMIND_TYPE_SILENT:静默;BasicCustomPushNotification.REMIND_TYPE_VIBRATE:震动;BasicCustomPushNotification.REMIND_TYPE_SOUND:声音;BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND:声音+震动
public BasicCustomPushNotification(int drawable, int flags, int remindType);
获取状态栏图标
- 获取已设置的状态栏图标
public int getStatusBarDrawable()
设置状态栏图标
- 更改状态栏图标设置
参数
- statusBarDrawable 状态栏图标资源id
public void setStatusBarDrawable(int statusBarDrawable);
获取提醒方式
- 获取已经设置的提醒方式
public int getRemindType();
设置提醒方式
- 更改自定义通知的提醒方式
参数
- remindType 提醒方式,提供的参数有:BasicCustomPushNotification.REMIND_TYPE_SILENT:静默;BasicCustomPushNotification.REMIND_TYPE_VIBRATE:震动;BasicCustomPushNotification.REMIND_TYPE_SOUND:声音;BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND:声音+震动
public void setRemindType(int remindType);
获取Notification Flags参数
- 获取已经设置的notification flag参数
public int getNotificationFlags();
设置Notification Flags参数
- 更改自定义通知的flags参数
参数
- notificationFlags 支持系统自带的Notification Flag参数
public void setNotificationFlags(int notificationFlags);
获取是否服务端设置优先
- 利用OpenApi或者阿里云推送控制台推送消息都可以设置提醒方式,当后端设置的提醒方式和自定义样式提醒方式冲突时,SDK根据serverOptionFirst参数来判断提醒方式策略。如果该参数为true,则采用后端设定的提醒方式;如果该参数为false,则采用自定义样式指定的提醒方式。默认为false
public boolean isServerOptionFirst();
设置是否服务端优先
- 更改自定义通知的serverOptionFirst参数
参数
- serverOptionFirst 是否服务器配置优先
public void setServerOptionFirst(boolean serverOptionFirst);
获取推送前台到达否创建通知参数
- 当推送到达时,如果应用处在前台,用户可以通过自定义样式决定是否创建通知。默认是创建通知
public boolean isBuildWhenAppInForeground();
设置推送前台到达否创建通知参数
- 更改当推送到达时应用处在前台情况下是否创建通知的设置
参数
- buildWhenAppInForeground 是否创建通知
public void setBuildWhenAppInForeground(boolean buildWhenAppInForeground);
3.5 AdvancedCustomPushNotification API
AdvancedCustomPushNotification
是BasicCustomPushNotification
的子类,继承了上文中BasicCustomPushNotification
的所有方法。
AdvancedCustomPushNotification构造函数
- AdvancedCustomPushNotification类的构造函数,AdvancedCustomPushNotification没有默认构造函数
参数
- view 自定义通知布局文件id。注:Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局。
- iconViewId 自定义布局文件中icon的viewId
- titleViewId 自定义布局文件中title的viewId
- contentViewId 自定义布局文件中显示通知正文的viewId
public AdvancedCustomPushNotification( int view, int iconViewId, int titleViewId, int contentViewId);
设置通知图标
- 设置通知栏中显示的图标,该图标显示在iconViewId所指定的控件中。
参数
- icon icon图标资源id
public void setIcon(int icon);
获取通知图标
- 获取设置的通知图标
public int getIcon();
3.6 CustomNotificationBuilder API
- CustomNotificationBuilder用于注册用户设定好的自定义样式通知
获取CustomNotificationBuilder实例
- CustomNotificationBuilder是单例类,必须通过指定接口来获取实例
public static CustomNotificationBuilder getInstance();
注册自定义样式通知
- 用户创建好自定义样式通知后需要将其注册,并赋予其一个特定的id
参数
- customNotificationId 所注册的自定义样式通知的id,id必须大于0。如果将多个不同的自定义样式通知赋予同一个id,则最后注册的通知有效,其他的通知将会被覆盖
- notification 创建的通知,该通知可以是BasicCustomPushNotification对象也可以是AdvancedCustomPushNotification对象,但是不能为null
返回
- 该方法会返回一个boolean类型的结果,如果返回true,则注册成功;反之则失败。
public boolean setCustomNotification(int customNotificationId, BasicCustomPushNotification notification);
最后更新:2016-12-15 18:24:57
上一篇:
Android SDK配置__Android SDK手册_App SDK 手册_移动推送-阿里云
下一篇:
移动推送辅助通道配置__Android SDK手册_App SDK 手册_移动推送-阿里云
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云