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"
<service
android:name=".service.PushService"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
解决思路:点击进入mPushAgent.setPushIntentServiceClass(PushService.class)
方法
public <U extends UmengMessageService> void setPushIntentServiceClass(Class<U> var1) {
可知参数PushService
必须继承至UmengMessageService
,再点击进入类UmengMessageService
public abstract class UmengMessageService extends UMJobIntentService {
public class UMJobIntentService extends JobIntentService {
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
,两者不能同时设置生效
//mPushAgent.setMessageHandler(MyUmengMessageHandler.getInstance(context));
//mPushAgent.setNotificationClickHandler(notificationClickHandler);
mPushAgent.setPushIntentServiceClass(PushService.class);
android:process
切勿设置
android:process=":push"
否则独立进程获得全局变量的值与主进程产生不同步现象
Time-out while trying to bind Service
如果发现报错:
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
方法
//@Nullable
//@Override
//public IBinder onBind(Intent intent) {
// return null;
//}
构建通知栏
从Android 8.0开始,NotificationManager需要创建NotificationChannel,然后调用createNotificationChannel把NotificationChannel传递进去,并且通过setChannelId设置相应的id
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());
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/android-8-and-above-integrate-with-umeng-push/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论