美文网首页
Android保活,杀死app后收到推送,推送收不到

Android保活,杀死app后收到推送,推送收不到

作者: 蹦蹦跳跳猪猪侠 | 来源:发表于2020-07-14 14:21 被阅读0次

Android保活,杀死app后收到推送,推送收不到。

其实指向的都是一个问题,杀死app后,我还要能收到推送。

用阿里推送的辅助通道功能可以实现

https://help.aliyun.com/document_detail/30067.html?spm=a2c4g.11186623.6.590.598b7fa8XmiUlS#h2-7-

辅助通道一般只接入,小米,华为,oppo,vivo,魅族这5个厂商的,

其中的一些厂商需要额外的依赖。

阿里的文档没有给出正确的依赖,

正确的,包含5大厂商的依赖如下:

api 'com.aliyun.ams:alicloud-android-push:3.1.9@aar'

api 'com.aliyun.ams:alicloud-android-utils:1.1.5'

api 'com.aliyun.ams:alicloud-android-beacon:1.0.3'

api 'com.aliyun.ams:alicloud-android-ut:5.4.3'

api 'com.aliyun.ams:alicloud-android-third-push:3.0.8@aar'

api 'com.aliyun.ams:huawei-push:2.6.3.305'

api 'com.aliyun.ams:huawei-push-base:2.6.3.305'

api 'com.aliyun.ams:meizu-push:3.8.3-fix'

阿里sdk的使用主要通过这个类

public class PushService {

private static boolean IS_INIT_PUSH = false;

/**

* 初始化云推送通道

*

* @param applicationContext

*/

public static void registerPushService(Context applicationContext) {

PushServiceFactory.init(applicationContext);

final CloudPushService pushService = PushServiceFactory.getCloudPushService();

// pushService.setDebug(BuildConfig.DEBUG);

if (!IS_INIT_PUSH){

pushService.register(applicationContext, new CommonCallback() {

@Override

public void onSuccess(String response) {

Log.d("Push", "init cloudchannel success "+response);

IS_INIT_PUSH = true;

pushServiceBindAccount(applicationContext);

}

@Override

public void onFailed(String errorCode, String errorMessage) {

Log.d("Push", "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);

}

});

}

}

public static void pushServiceBindAccount(Context applicationContext) {

final CloudPushService pushService = PushServiceFactory.getCloudPushService();

String token = String.valueOf(PAccountManage.INSTANCE.getSp().getAccountId());

//需要绑定到账号

if (IS_INIT_PUSH && !TextUtils.isEmpty(token)) {

pushService.bindAccount(token, new CommonCallback() {

@Override

public void onSuccess(String s) {

Log.d("Push", "bindAccount onSuccess");

initThirdPush(applicationContext);

}

@Override

public void onFailed(String s, String s1) {

Log.d("Push", "bindAccount onFailed");

}

});

}else {

Log.d("Push", "bindAccount onFailed2");

}

}

public static void pushServiceUnBindAccount(Context applicationContext) {

final CloudPushService pushService = PushServiceFactory.getCloudPushService();

if (IS_INIT_PUSH){

pushService.unbindAccount(new CommonCallback() {

@Override

public void onSuccess(String s) {

Log.d("Push", "unbindAccount onSuccess");

unRegisterThirdPush(applicationContext);

}

@Override

public void onFailed(String s, String s1) {

Log.d("Push", "unbindAccount onFailed");

}

});

}else {

Log.d("Push", "unbindAccount onFailed2");

}

}

private static void initThirdPush(Context applicationContext) {

// 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。

MiPushRegister.register(applicationContext, "", "");

// 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。

HuaWeiRegister.register((Application)applicationContext );

//GCM/FCM辅助通道注册

// GcmRegister.register(applicationContext, "", ""); //sendId/applicationId为步骤获得的参数

// OPPO通道注册

OppoRegister.register(applicationContext, "", ""); // appKey/appSecret在OPPO通道开发者平台获取

// VIVO通道注册

VivoRegister.register(applicationContext);

//魅族

MeizuRegister.register(applicationContext, "", ""); // appId/appkey在魅族开发者平台获取

}

private static void unRegisterThirdPush(Context applicationContext){

MiPushRegister.unregister(applicationContext);

// HuaWeiRegister.registerBundle((Application)applicationContext ,false);

OppoRegister.unregister(); // appKey/appSecret在OPPO通道开发者平台获取

VivoRegister.unregister();

// MeizuRegister.register(applicationContext, "", ""); // appId/appkey在魅族开发者平台获取

}

}

打开app需要初始化阿里sdk,Application里:

override fun onCreate() {

initPushModule()

}

private fun initPushModule() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

PushManager.initChannel()

}

if ( (isMainProcess(this@ParentApp) || isTargetProcess(this@ParentApp, "${applicationContext.packageName}:channel"))) {

PushService.registerPushService(this@ParentApp)

}

}

用户登录时调用:

PushService.pushServiceBindAccount(this)

退出登录时调用:

PushService.pushServiceUnBindAccount(this)

创建通道的代码:

// 通知渠道的id

var mChannelId = "aierman"

// 用户可以看到的通知渠道的名字.

private var mChannelName = "哈喽萝博推送通知"

// 用户可以看到的通知渠道的描述

private var mChannelDescription = "哈喽萝博推推送通知"

fun initChannel() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

val notificationManager = BaseApp.app.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val channel = NotificationChannel(mChannelId, mChannelName, NotificationManager.IMPORTANCE_HIGH)

// 配置通知渠道的属性

channel.description = mChannelDescription

// 设置通知出现时的闪灯(如果 android 设备支持的话)

channel.enableLights(true)

channel.lightColor = Color.RED

// 设置通知出现时的震动(如果 android 设备支持的话)

channel.enableVibration(true)

channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)

// 设置通知出现时声音,默认通知是有声音的

channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, Notification.AUDIO_ATTRIBUTES_DEFAULT);

channel.setShowBadge(true)//打开脚标

notificationManager.createNotificationChannel(channel)

}

}

别忘了在清单文件加入以下配置:

<meta-data

android:name="com.huawei.hms.client.appid"

android:value="" />

<meta-data

android:name="com.vivo.push.api_key"

android:value="" />

<meta-data

android:name="com.vivo.push.app_id"

android:value="" />

<meta-data

android:name="com.alibaba.app.appkey"

android:value="" /> <!-- 阿里推送 appSecret -->

<meta-data

android:name="com.alibaba.app.appsecret"

android:value="" /> <!-- 消息接收监听器 (用户可自主扩展) -->

<activity

android:name="com.hellorobotedu.aiparent.push.PopupPushActivity"

android:exported="true"/>

对于PopupPushActivity,记得:

class PopupPushActivity : AndroidPopupActivity()

最后一定要记得在阿里推送控制台的应用配置,配置你各个手机厂商的key,不然辅助通道不会生效。

坑:

1.华为,记得配置SHA256,不然辅助通道不生效见图

image.png

2.推送不到达的意外情况处理:

(1).杀死app后立即进行推送是收不到的,因为阿里那边不知道app已经死了,

辅助弹窗和正式通道都推不过来。

比如杀死app后立即推送了一个“推送a”,那app肯定是收不到的,

那只能等下一次有效的推送(有效指的是离杀死app的时间足够久的推送),比如“推送b”,

“推送b”推送了之后,“推送a”和”推送b”才可以一起收到.

(2).切换账号问题,

举例,我已经在oppo手机登录,杀死app,

再在小米手机登录,再杀死app

那么再次推送还是会推到oppo,因为oppo和阿里的绑定没有解除,

如何解除?在oppo上执行退出登录,或者在小米手机上运行app足够久。

end

如果你觉得这篇文章对你有所帮助,不妨点一个赞,作者会非常高兴的。

相关文章

网友评论

      本文标题:Android保活,杀死app后收到推送,推送收不到

      本文链接:https://www.haomeiwen.com/subject/ngaxhktx.html