版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
详细解答可以参考官方帮助文档
Android SDK最新版本v3.0.11。
API索引:
以下接口调用时,如有回调,均为异步执行,且回调不能为空。
ApplicationContext)
void register(Context context, CommonCallback callback);
ApplicationContext)
void register(Context context, String appKey, String appSecret, CommonCallback callback);
void onAppStart();
String getDeviceId();
CloudPushService.ERROR;CloudPushService.ERROR | CloudPushService.INFO | CloudPushService.DEBUG | CloudPushService.OFF(关闭Log)
void setLogLevel(int logLevel);
V2.3.6及以上版本支持)register注册接口之前调用该接口,否则动态设置失效
void setAppKey(String appKey);
V2.3.6及以上版本支持)register注册接口之前调用该接口,否则动态设置失效
void setAppSecret(String appSecret);
V3.0.3及以上版本支持)
void turnOnPushChannel(CommonCallback callback);
V3.0.3及以上版本支持)
void turnOffPushChannel(CommonCallback callback);
V3.0.3及以上版本支持)
void checkPushChannelStatus(CommonCallback callback);
V3.0.10及以上版本支持)
void setPushIntentService(Class messageIntentService);
64字节。
void bindAccount(String account, CommonCallback callback);
void unbindAccount(CommonCallback callback);
target指定;1万个标签,单个标签最大支持128字符。V2.3.5及以上版本)        目标类型,CloudPushService.DEVICE_TARGET:本设备;    CloudPushService.ACCOUNT_TARGET:本账号;    CloudPushService.ALIAS_TARGET:别名
void bindTag(int target, String[] tags, String alias, CommonCallback callback);
target指定;解绑标签只是解除设备和标签的绑定关系,不等同于删除标签,即该APP下标签仍然存在,系统目前不支持标签的删除。V2.3.5及以上版本)        目标类型,CloudPushService.DEVICE_TARGET:本设备;    CloudPushService.ACCOUNT_TARGET:本账号;    CloudPushService.ALIAS_TARGET:别名
void unbindTag(int target, String[] tags, String alias, CommonCallback callback);
V2.3.5及以上版本)        目标类型,CloudPushService.DEVICE_TARGET:本设备;
void listTags(int target, CommonCallback callback);
128个别名,且同一别名最多添加到128个设备;128字节。
void addAlias(String alias, CommonCallback callback);
alias = null or alias.length = 0时,删除设备全部别名)
void removeAlias(String alias, CommonCallback callback);
void listAliases(CommonCallback callback);
R.raw.alicloud_notification_sound的资源文件;
void setNotificationSoundFilePath(String filePath);
void setNotificationLargeIcon(Bitmap icon);
void setNotificationSmallIcon(int iconId);
以下功能支持该用法,V2.3.5及以上版本使用关闭免打扰功能接口)
void setDoNotDisturb(int startHour, int startMinute, int endHour, int endMinute, CommonCallback callback);
V2.3.5及以上版本支持)免打扰功能时段功能后自动打开免打扰功能
void closeDoNotDisturbMode();
V2.3.7及以上版本支持)
void clearNotifications();
V3.0.6及以上版本支持)onMessage回调中自行创建通知)的删除/点击事件上报,其相关实现可以参考移动推送Android SDK:透传消息+用户自建通知最佳实践,如果您直接通过阿里云推送通知,无需使用相关接口。
void clickMessage(CPushMessage message);
void dismissMessage(CPushMessage message);
V3.0.11及以上版本支持)为提高信息的到达率和实效性,扩展推送的使用场景,我们推出了推送与短信的融合通知模式。开发者可以设置在一定时间内,如果用户未收到或未点击推送,通过短信补发通知用户。具体方案可参考:推送短信融合。
为实现推送短信融合方案,需要在终端接入绑定/解绑电话号接口,接口详情如下。
将设备与电话号绑定
void bindPhoneNumber(String phoneNumber, CommonCallback callback);
解除当前设备与电话号的绑定关系
void unbindPhoneNumber(CommonCallback callback);
MessageReciever,可以拦截通知,接收消息,获取推送中的扩展字段。或者在通知打开或删除的时候,切入进行后续处理。com.alibaba.sdk.android.push.AliyunMessageIntentService,并覆写相关方法,AliyunMessageIntentService所有消息回调同MessageReceiver一致。使用方法:
MessageReceiver
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>
com.alibaba.sdk.android.push.AliyunMessageIntentService并覆写相关方法
<service android:name="MyPushIntentService" ><intent-filter><action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" /></intent-filter><intent-filter><action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" /></intent-filter><intent-filter><action android:name="com.alibaba.sdk.android.push.RECEIVE" /></intent-filter></service>
CPushMessage类型,可以获取消息Id、消息标题和内容。
void onMessage(Context context, CPushMessage message);
_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)
_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);
v2.3.2及以上版本支持),通知打开上报由SDK自动完成。_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);
void onNotificationRemoved(Context context, String messageId);
onNotification回调(v2.3.3及以上版本支持)
void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl);
V2.3.3及以上版本开始支持)Android Push SDK支持用户自定义通知样式,用户可以设定自己的通知样式,涉及的内容包括通知的提醒方式(声音、震动、静默),通知在状态栏的显示图标,推送消息应用内到达时是否创建通知以及自定义通知布局文件等。自定义样式通知的设置包括两部分:
BasicCustomPushNotification;2)AdvancedCustomPushNotification。其中BasicCustomPushNotification用户设置基础样式,包括提醒方式、状态栏图标以及当推送消息到达时应用正处于前台情况下是否创建该通知等。AdvancedCustomPushNotification是BasicCustomPushNotification的子类,继承了BasicCustomPushNotification的所有方法,同时还可以设置通知样式布局文件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);
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().setCustomNotification(2, notification);
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"); // 通知的摘要pushRequest.setAndroidNotificationBarType(2);//设置的通知样式ID,通知栏自定义样式范围0-100// 推送配置: AndroidpushRequest.setAndroidOpenType("1"); // 点击通知后动作,1:打开应用 2: 打开应用Activity 3:打开 urlpushRequest.setAndroidExtParameters("{\"_NOTIFICATION_BAR_STYLE_\":\"2\"}");
public BasicCustomPushNotification();
public BasicCustomPushNotification(int drawable, int flags, int remindType);
public int getStatusBarDrawable()
public void setStatusBarDrawable(int statusBarDrawable);
public int getRemindType();
public void setRemindType(int remindType);
public int getNotificationFlags();
public void setNotificationFlags(int notificationFlags);
public boolean isServerOptionFirst();
public void setServerOptionFirst(boolean serverOptionFirst);
public boolean isBuildWhenAppInForeground();
public void setBuildWhenAppInForeground(boolean buildWhenAppInForeground);
AdvancedCustomPushNotification是BasicCustomPushNotification的子类,继承了上文中BasicCustomPushNotification的所有方法。
public AdvancedCustomPushNotification( int view, int iconViewId, int titleViewId, int contentViewId);
public void setIcon(int icon);
public int getIcon();
public static CustomNotificationBuilder getInstance();
public boolean setCustomNotification(int customNotificationId, BasicCustomPushNotification notification);