开发中总会遇到产品经理需求是:结束掉应用依然想接收到通知,iOS轻而易举就可以实现,但Android一直都是杀掉应用后就无法接收到通知!
网上给的结果大概是:
1、集成各大厂商的推送sdk;(太繁琐)
2、集成某一大品牌的推送sdk,通过应用之间相互调用的方案;(测试无效)
3、等待Android统一推送联盟方案出来;(时间未知)
今天告诉你一个方案,通过阿里推送的辅助渠道推送可以实现即使杀掉应用也可以接收到通知:
官方辅助渠道集成文档地址
提前准备:
1、各大开放平台的应用申请(小米、华为、OPPO、VIVO、魅族);
注意:有些开放平台是分开 push 功能的,需要在 push 功能区 开通/启用 推送功能
2、集成阿里推送sdk文档;
3、在阿里云移动推送配置渠道平台的信息,如下图:
4、辅助渠道推送也需要后台服务器进行配合,具体参考移动推送辅助通道配置 的7.3场景解析中的(场景1:普通推送打开App + 辅助弹窗 或 场景2:普通推送打开Activity + 辅助弹窗)
集成流程很简单,就依照文档集成完全没问题:
下面我记录我自己的集成,不做说明(我只集成了小米、华为、oppo、vivo):
一、项目的gradle配置:
allprojects {
repositories {
//阿里推送需要
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases'
}
}
}
app的gradle配置:
dependencies {
//阿里推送
api('com.aliyun.ams:alicloud-android-push:3.1.6') {
transitive true
}
//阿里移动推送辅助通道
api 'com.aliyun.ams:alicloud-android-third-push:3.0.9@aar'
//华为推送SDK依赖
api 'com.aliyun.ams:huawei-push:2.6.3.305'
api 'com.aliyun.ams:huawei-push-base:2.6.3.305'
}
二、app模块的AndroidManifest.xml中的配置:
<!-- 阿里的移动推送 -->
<meta-data
android:name="com.alibaba.app.appkey"
android:value="xxxxxx" /> <!-- 请填写你自己的- appKey -->
<meta-data
android:name="com.alibaba.app.appsecret"
android:value="xxxxxx" /> <!-- 请填写你自己的appSecret -->
<!--华为推送-->
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="appid=xxxxxx" />
<!--VIVO推送-->
<meta-data
android:name="com.vivo.push.api_key"
android:value="xxxxxx" />
<meta-data
android:name="com.vivo.push.app_id"
android:value="xxxxxx" />
三、项目的application中的初始化:
/**
* 初始化阿里云推送通道
*/
private void initCloudChannel() {
PushServiceFactory.init(this);
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.setDebug(true);
pushService.register(this, new CommonCallback() {
@Override
public void onSuccess(String response) {
LogUtil.d("init cloudchannel success");
}
@Override
public void onFailed(String errorCode, String errorMessage) {
LogUtil.d("init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
}
});
}
/**
* 初始化辅助推送渠道
*/
private void initThirdPushChannel() {
// 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
MiPushRegister.register(this, AppConstant.XIAOMI_APP_ID, AppConstant.XIAOMI_APP_Key);
// 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。
HuaWeiRegister.register(this);
// OPPO通道注册
OppoRegister.register(this, AppConstant.OPPO_APP_KEY, AppConstant.OPPO_APP_SECRET); // appKey/appSecret在OPPO开发者平台获取
// // 魅族通道注册
// MeizuRegister.register(applicationContext, "appId", "appkey"); // appId/appkey在魅族开发者平台获取
// VIVO通道注册
VivoRegister.register(this);
}
四、代码接收推送消息:
1、正常推送接收代码AliMessageReceiver:
public class AliMessageReceiver extends MessageReceiver {
private String channelId;
private NotificationManager notificationManager;
@Override
public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
//处理推送通知
LogUtil.e("Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
}
@Override
public void onMessage(Context context, CPushMessage cPushMessage) {
//处理推送消息
LogUtil.e("onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
}
@Override
public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
//点击通知后的操作
LogUtil.e("onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
LogUtil.e("onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
LogUtil.e("onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
}
@Override
protected void onNotificationRemoved(Context context, String messageId) {
//处理移除通知栏
LogUtil.e("onNotificationRemoved");
}
}
2、辅助渠道接收代码PopupPushActivity(注意:此activity的绝对路径需要在服务端的代码中配置):
public class PopupPushActivity extends AndroidPopupActivity {
static final String TAG = "PopupPushActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.anim_no, R.anim.anim_no);
}
@Override
protected void onSysNoticeOpened(String title, String summary, Map<String, String> extMap) {
//点击通知栏后执行的方法
// Log.e("sdsd", "OnMiPushSysNoticeOpened, title: " + title + ", content: " + summary + ", extMap: " + extMap);
}
}
到这里就集成完了,可以去测试了,至于文档中(6. 在日志中查看初始化情况 )我集成后有些设备并没有输出集成成功的标志(如红米手机),所以至于检测是否初始化成功,有最好,没有就直接去测试推送,不要过分纠结浪费时间!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)