2018年二月的第一天,我又开始装逼啦,Android8.0出来已经有一段时间了,前段时间拿8.0的系统运行了一下自己的APP发现推送收不到,瞬间尴尬
,查找了开发者文档发现Google大佬们对8.0的通知有了“哒哒滴”改善,今天就拿我解决推送的方法和大家分享一下,灰常滴简单,因为项目中以前的推送使用的是第三方极光推送,也不知道现在极光有木有支持8.0,呵呵,很大的原因是我觉得再重新集成新的jar包麻烦,也就没有去关注这个有木有更新。接下来要开始我的表演啦!
首先奉上官方文档的链接:点这里
public class JPushBtsReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.e("DDZTAG","收到广播了"+Build.VERSION.SDK_INT); Bundle bundle = intent.getExtras(); //加一个推送管理类 if(Build.VERSION.SDK_INT >=26){ Log.e("DDZTAG","8.0处理了"); NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); String id=context.getPackageName(); CharSequence name=context.getString(R.string.app_name); int importance=NotificationManager.IMPORTANCE_HIGH; NotificationChannel channel=new NotificationChannel(id,name,importance); channel.enableLights(true); channel.setLightColor(Color.RED); channel.enableVibration(true); channel.setShowBadge(true); channel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400}); notificationManager.createNotificationChannel(channel); int notificationId = 0x1234; Notification.Builder builder = new Notification.Builder(context,id); //与channelId对应 //icon title text必须包含,不然影响桌面图标小红点的展示 String message=bundle.getString(JPushInterface.EXTRA_ALERT); builder.setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("消息的title") .setContentText(message); builder.setAutoCancel(true); String FunctionId = ""; String ParamId = ""; String MsgId = ""; String json = bundle.getString(JPushInterface.EXTRA_EXTRA); try { JSONObject job = new JSONObject(json); FunctionId = job.optString(""); ParamId = job.optString(""); MsgId = job.optString(""); Intent i = DDZConsts.getAimIntent(context,Integer.valueOf(FunctionId),ParamId,MsgId); if(MsgId != null && MsgId.length() > 0){ i.putExtra(DDZConsts.INTENT_EXTRA_MSG_ID,MsgId); } i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0); builder.setContentIntent(pendingIntent); notificationManager.notify(Integer.valueOf(FunctionId), builder.build()); } catch (Exception e) { e.printStackTrace(); Log.i("DDZTAG", "极光推送出错:" + e.getMessage()); } }else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { String FunctionId = ""; String ParamId = ""; String MsgId = ""; String json = bundle.getString(JPushInterface.EXTRA_EXTRA); try { JSONObject job = new JSONObject(json); FunctionId = job.optString(""); ParamId = job.optString(""); MsgId = job.optString(""); Intent i = DDZConsts.getAimIntent(context,Integer.valueOf(FunctionId),ParamId,MsgId); if(MsgId != null && MsgId.length() > 0){ i.putExtra(DDZConsts.INTENT_EXTRA_MSG_ID,MsgId); } i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } catch (Exception e) { e.printStackTrace(); Log.i("DDZTAG", "极光推送出错:" + e.getMessage()); } } } }
有木有很简单,就是简单的区分一下手机的版本是不是8.0或者更高版本,如果是就进8.0的处理逻辑,否则还用以前的处理逻辑。这样也算是一种偷懒对不。
其中的一些逻辑,也非常的简单,官方文档也有很详细的说明,在这我就不再用我那拙劣的文笔描述了,如果有哪些地方不明白可以在评论区评论出来,大家一起交流,互相提高。