本文介绍 Android SDK 4.x 厂商通道通知点击的中转 Activity 配置,以及通过统一回调处理点击业务的方法。
适用范围
本文示例以 Android SDK 4.0.0 为基准,针对小米、华为、荣耀、OPPO、vivo、魅族等使用辅助弹窗的厂商通知。FCM data 消息由移动推送 SDK 直接处理,无需此中转 Activity。3.x 用户请使用辅助弹窗接入(3.x);从 3.x 升级时,先迁移旧回调业务。
4.x 仍需要辅助弹窗 Activity。变化的是点击业务回调位置:数据解析成功后,由 MessageReceiver 或 AliyunMessageIntentService 的 onNotificationOpened 接收。
不再实现 onSysNoticeOpened、onNotPushData、onParseFailed 或 PopupNotifyClickListener 处理点击业务。
准备工作
-
已完成 Android SDK 4.x 主包接入,并完成相关厂商平台和 EMAS 控制台配置。
-
主 SDK、厂商基础适配包及对应厂商适配包使用配套版本。4.0.0 接入请使用配套的 4.0.0 适配包。
-
在应用启动时完成消息接收器或 IntentService 配置,确保应用由厂商通知冷启动时,也能完成必要初始化。
第一步:配置厂商通知点击的中转 Activity
以下两种方式二选一。Activity 的全限定类名必须与 AndroidManifest.xml 和服务端推送参数一致。
方式一:继承 AndroidPopupActivity
package com.example.push;
import com.alibaba.sdk.android.push.AndroidPopupActivity;
public class MyPopupActivity extends AndroidPopupActivity {
// 4.x 中作为厂商通知点击的中转入口,无需实现 onSysNoticeOpened。
}
方式二:在已有 Activity 中使用 PopupNotifyClick
如果业务 Activity 已继承自应用的 BaseActivity,可保留其继承关系并加入下列生命周期调用。示例使用 Activity 展示所需方法。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.alibaba.sdk.android.push.popup.PopupNotifyClick;
public class MyPopupActivity extends Activity {
private final PopupNotifyClick popupNotifyClick = new PopupNotifyClick();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popupNotifyClick.onCreate(this, getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
popupNotifyClick.onNewIntent(intent);
}
}
两种方式均在 AndroidManifest.xml 的 application 节点下注册 Activity:
<activity
android:name="com.example.push.MyPopupActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="${applicationId}"
android:path="/thirdpush"
android:scheme="agoo" />
</intent-filter>
</activity>
请将示例包名替换为应用中的实际类名。已有 Activity 的布局、路由和生命周期逻辑按应用需要保留。
第二步:配置统一点击回调
Android SDK 4.x 中,MessageReceiver 和 AliyunMessageIntentService 的业务回调范围如下:
ACCS 自有通道和 FCM:支持消息接收(onMessage)、通知到达(onNotification)、通知点击(onNotificationOpened)、通知清除(onNotificationRemoved)等完整业务回调。
除 FCM 外的其他 Android 厂商通道:仅支持 onNotificationOpened(通知点击),不触发消息接收、通知到达、通知清除等其他回调。
以下两种接收方式二选一。onNotificationOpened 的第四个参数为 Map<String, String>,无需再按 JSON 字符串解析。
方式一:使用 MessageReceiver
package com.example.push;
import android.content.Context;
import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import java.util.Map;
public class MyMessageReceiver extends MessageReceiver {
@Override
public void onMessage(Context context, CPushMessage message) {
// 按应用需要处理透传消息。
}
@Override
public void onNotification(Context context, String title,
String summary, Map<String, String> extraMap) {
// 按应用需要处理通知到达。
}
@Override
public void onNotificationRemoved(Context context, String messageId) {
// 按应用需要处理通知删除。
}
@Override
public void onNotificationOpened(Context context, String title,
String summary, Map<String, String> extraMap) {
String orderId = extraMap == null ? null : extraMap.get("orderId");
// 自有通道和厂商通道通知点击统一在此处理。
// 根据 orderId 等业务参数,交由应用的页面路由逻辑处理。
}
}<receiver
android:name="com.example.push.MyMessageReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
<action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
<action android:name="com.alibaba.sdk.android.push.RECEIVE" />
</intent-filter>
</receiver>
方式二:使用 AliyunMessageIntentService
package com.example.push;
import android.content.Context;
import com.alibaba.sdk.android.push.AliyunMessageIntentService;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import java.util.Map;
public class MyMessageIntentService extends AliyunMessageIntentService {
@Override
public void onMessage(Context context, CPushMessage message) {
// 按应用需要处理透传消息。
}
@Override
public void onNotification(Context context, String title,
String summary, Map<String, String> extraMap) {
// 按应用需要处理通知到达。
}
@Override
public void onNotificationRemoved(Context context, String messageId) {
// 按应用需要处理通知删除。
}
@Override
public void onNotificationOpened(Context context, String title,
String summary, Map<String, String> extraMap) {
String orderId = extraMap == null ? null : extraMap.get("orderId");
// 在此处理通知点击业务。
}
}<service
android:name="com.example.push.MyMessageIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
<action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
<action android:name="com.alibaba.sdk.android.push.RECEIVE" />
</intent-filter>
</service>
在 PushServiceFactory.init 之后、开始注册之前设置服务类:
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.setPushIntentService(MyMessageIntentService.class);
设置 setPushIntentService 后,消息和通知回调由该 IntentService 处理,不再通过 MessageReceiver 处理。未设置时默认使用 MessageReceiver。
厂商通知点击由中转 Activity 解析后,统一通过 onNotificationOpened 回调交给应用处理业务。请在该回调中集中处理页面路由。
第三步:配置服务端推送参数
|
接口 |
厂商点击入口 |
通知标题与正文 |
|
Push / MassPush |
AndroidPopupActivity:填写中转 Activity 的全限定类名 |
使用 AndroidPopupTitle / AndroidPopupBody 设置厂商通知内容;同时填写 Title / Body。 |
|
PushV2 / MassPushV2 |
PushTask.Notification.Android.VendorChannelActivity:填写同一中转 Activity 的全限定类名 |
使用 PushTask.Notification.Title / Body。 |
使用 Push 接口时,请同时按接口要求配置 StoreOffline 和目标设备等基础参数。通过控制台发送时,在厂商设置中填写对应点击 Activity 和通知内容。
完成客户端回调迁移后,仍需按本节配置服务端厂商通知参数。
接入验证
-
在 PushServiceFactory.init 后、register 和厂商注册前,显式调用 setLogLevel(CloudPushService.LOG_DEBUG)。
-
分别测试自有通道与厂商通道通知点击,检查统一的 onNotificationOpened 是否收到标题、正文和业务参数。
-
测试应用运行中、从后台恢复和由通知冷启动的点击路径,确认实际业务页面和返回行为符合预期。
-
在 Logcat 中按 TAG EMAS_PUSH 过滤,再查找正文中的 [PopupNotifyClick] 和 sendNotificationOpenedIntent,定位 SDK 点击分发日志,并继续验证应用业务回调。
-
厂商原生 SDK 和 Android 系统日志请按各自的 TAG 查询。