前言
通过集成MessageReceiver/AliyunMessageIntentService,可以拦截通知,接收消息,获取推送中的扩展字段。或者在通知打开或删除的时候进行后续处理。
MessageReceiver和AliyunMessageIntentService是处理推送数据的两种实现方式,它们具有同样的扩展接口,您仅需要任选一种方式即可。
集成MessageReceiver(二选一)
1 实现自定义的MessageReceiver
继承com.alibaba.sdk.android.push.MessageReceiver。
2 注册自定义的Messagereceiver
在您的AndroidManifest.xml文件(通常是<project>/<app-module>/src/main/AndroidManifest.xml)中,在application节点下注册自定义的MessageReceiver。
<!--消息接收监听器-->
<receiver android:name="com.alibaba.sdk.android.push.MessageReceiver"
android:exported="false"> <-- 在这里替换成您自己的receiver -->
<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>
</receiver>
3 处理推送通知和推送消息
在MessageReceiver内可以处理推送通知、推送消息和通知点击事情,具体API请分别查看推送消息类型和推送通知类型。
集成AliyunMessageIntentService(二选一)
1 实现自定义的MessageIntentService
继承com.alibaba.sdk.android.push.AliyunMessageIntentService。
2 注册自定义的MessageIntentService
在您的AndroidManifest.xml文件(通常是<project>/<app-module>/src/main/AndroidManifest.xml)中,在application节点下注册自定义的MessageIntentService。
<service android:name="com.alibaba.sdk.android.push.AliyunMessageIntentService"
android:exported="false"> <-- 在这里替换成您自己的service -->
<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>
3 设置使用MessageIntentService
SDK默认是支持MessageReceiver处理消息的,如果要使用MessageIntentService,您需要调用
CloudPushService的setPushIntentService。
必须在应用的Application#onCreate()方法中执行setPushIntentService设置逻辑,并建议在推送SDK初始化后尽早调用。请勿放在Activity、Service或其他业务回调中延迟执行,否则SDK可能已按默认MessageReceiver方式分发消息和通知回调,导致自定义MessageIntentService无法按预期生效。
val service = PushServiceFactory.getCloudPushService()
service.setPushIntentService(MyMessageIntentService.class) //在这里替换成您自己的servicefinal CloudPushService service = PushServiceFactory.getCloudPushService();
service.setPushIntentService(MyMessageIntentService.class); //在这里替换成您自己的service4 处理推送通知和推送消息
在MessageIntentService内可以处理推送通知、推送消息和通知点击事情,具体API请分别查看推送消息类型和推送通知类型。
推送消息类型
用于接收服务端推送的消息,消息不会弹窗,而是回调该方法。
如果您要通过OpenApi/控制台推送消息类型,必须在这个回调方法里处理消息数据,SDK默认不会处理消息类型数据。
onMessage
收到推送消息的回调,可以在这里处理推送消息。
接口定义
void onMessage(Context context, CPushMessage message)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
说明 |
|
context |
Context |
android组件上下文。 |
|
message |
CPushMessage |
推送的消息数据结构。 |
推送通知类型
用于接收服务端推送的通知,通知SDK会默认弹出通知,接收通知以及点击通知等都有回调。
SDK默认会处理推送通知类型,以通知形式进行交互。同时也支持拦截通知和自定义通知样式,但您需要扩展相应的API进行处理。
showNotificationNow
是否立即显示通知,此方法用于控制是否将推送通知交给SDK默认处理还是由用户自行处理。如果返回false,则您需要在onNotificationReceivedInApp内处理该推送通知。
接口定义
boolean showNotificationNow(Context context, Map<String, String> map)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
说明 |
|
context |
Context |
android组件上下文。 |
|
map |
Map<String, String> |
推送消息数据。 |
返回说明
|
类型 |
说明 |
|
boolean |
|
onNotification
推送通知到达回调。SDK收到通知后,回调该方法,可获取到并处理通知相关的参数。
接口定义
void onNotification(Context context, String title, String summary, Map<String, String> extraMap)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
说明 |
|
context |
Context |
android组件上下文。 |
|
title |
String |
通知标题。 |
|
summary |
String |
通知内容。 |
|
extraMap |
Map<String, String> |
通知额外参数,包括部分系统自带参数:
|
onNotificationOpened
点击通知回调。点击通知会回调该方法。
接口定义
void onNotificationOpened(Context context, String title, String summary, String extraMap)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
说明 |
|
context |
Context |
android组件上下文。 |
|
title |
String |
通知标题。 |
|
summary |
String |
通知内容。 |
|
extraMap |
String |
通知额外参数,JSON格式,包括部分系统自带参数:
|
onNotificationClickedWithNoAction
点击无跳转逻辑通知的回调,点击无跳转逻辑(open=4)通知时回调该方法(v2.3.2及以上版本支持)。
接口定义
void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
说明 |
|
context |
Context |
android组件上下文。 |
|
title |
String |
通知标题。 |
|
summary |
String |
通知内容。 |
|
extraMap |
String |
通知额外参数,JSON格式,包括部分系统自带参数:
|
onNotificationRemoved
删除通知的回调。删除通知时会回调该方法。
接口定义
void onNotificationRemoved(Context context, String messageId)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
是否必填 |
说明 |
|
context |
Context |
是 |
android组件上下文。 |
|
messageId |
String |
是 |
删除通知的Id。 |
onNotificationReceivedInApp
通知在应用内回调,该方法仅在showNotificationNow返回false时才会被回调,且此时不调用onNotification,此时需要您自己处理通知逻辑。
当用户创建自定义通知样式,并且设置推送应用内到达不创建通知弹窗时调用该回调。
接口定义
void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl)
所属类
MessageReceiver/AliyunMessageIntentService
参数说明
|
参数 |
类型 |
说明 |
|
context |
Context |
android组件上下文。 |
|
title |
String |
通知标题。 |
|
summary |
String |
通知内容。 |
|
extraMap |
Map<String, String> |
通知额外参数。 |
|
openType |
int |
原本通知打开方式,1:打开APP;2:打开activity;3:打开URL;4:无跳转逻辑。 |
|
openActivity |
String |
所要打开的activity的名称,仅当服务端参数openType=2时有效,其余情况为null。 |
|
openUrl |
String |
所要打开的URL,仅当服务端参数openType=3时有效,其余情况为null。 |