Android 8.0 及以上集成友盟推送采坑记录

setPushIntentServiceClass无效

采用mPushAgent.setPushIntentServiceClass(PushService.class)接收处理消息的方式,在友盟channel进程可以成功接收到消息,Logcat有消息日志打印,但在主进程接收不到消息

经测试,Android 8.0 及以上集成友盟推送接收及处理消息统计:

客户端消息处理方式 服务端发送消息类型 主进程是否成功接收消息
setMessageHandler notification
setMessageHandler message
setPushIntentServiceClass notification
setPushIntentServiceClass message

android:permission

Android 8.0 及以上务必添加权限:android:permission="android.permission.BIND_JOB_SERVICE"

1
2
3
4
<service
android:name=".service.PushService"
android:permission="android.permission.BIND_JOB_SERVICE"
/>

解决思路:点击进入mPushAgent.setPushIntentServiceClass(PushService.class)方法

1
public <U extends UmengMessageService> void setPushIntentServiceClass(Class<U> var1) {

可知参数PushService必须继承至UmengMessageService,再点击进入类UmengMessageService

1
public abstract class UmengMessageService extends UMJobIntentService {
1
public class UMJobIntentService extends JobIntentService {
1
public abstract class JobIntentService extends Service {

查看JobIntentService类源码发现有许多if (VERSION.SDK_INT >= 26)的判断,实际上在VERSION.SDK_INT >= 26,即Android 8.0以上采用JobScheduler机制实现服务调度,因此声明Service必须添加android:permission="android.permission.BIND_JOB_SERVICE"权限

自定义消息

如果只需接收自定义消息,并在客户端自行处理消息及构建通知栏,只需setPushIntentServiceClass,不能setMessageHandler,两者不能同时设置生效

1
2
3
//mPushAgent.setMessageHandler(MyUmengMessageHandler.getInstance(context));
//mPushAgent.setNotificationClickHandler(notificationClickHandler);
mPushAgent.setPushIntentServiceClass(PushService.class);

android:process

切勿设置

1
android:process=":push"

否则独立进程获得全局变量的值与主进程产生不同步现象

Time-out while trying to bind Service

如果发现报错:

1
2
2019-10-26 16:00:23.633 336-336/system_process W/JobServiceContext: Time-out while trying to bind 713f514 #u0a73/21000 com.lianlianpay.llterminal/.service.PushService, dropping.
2019-10-26 16:00:23.635 13259-13259/? I/yezhou: PushService.onDestroy

解决方法:注释或删除onBind方法

1
2
3
4
5
//@Nullable
//@Override
//public IBinder onBind(Intent intent) {
// return null;
//}

构建通知栏

从Android 8.0开始,NotificationManager需要创建NotificationChannel,然后调用createNotificationChannel把NotificationChannel传递进去,并且通过setChannelId设置相应的id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void buildNotification() {
manager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(mContext);
//解决Android8.0收不到消息问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(mChannel);
builder.setChannelId(PUSH_CHANNEL_ID);
}

Intent intent = new Intent(mContext, ReveiveEmailActivity.class);
Bundle bundle = new Bundle();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentTitle("appblog");
builder.setContentText("AppBlog.CN");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
builder.setAutoCancel(true);

manager.notify(id++, builder.build());
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :