这个问题。。。
是有点坑。。。。
项目中极光推送集成的比较早,所以就没在意,今天有用户反应收不到推送,哈哈,赶紧去官网看看,果然
首先替换了所有的jar文件进行升级
其次,推送收到后需要自己弹框显示,而且Android O版本中还添加了
NotificationChannel channel = new NotificationChannel("渠道id", "渠道名称",“渠道优先级”);
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
Log.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.e(TAG, "8.0处理了");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);//定义通知id
String channelId = context.getPackageName();//通知渠道id
String channelName = "消息通知";//"PUSH_NOTIFY_NAME";//通知渠道名
int importance = NotificationManager.IMPORTANCE_HIGH;//通知级别
NotificationChannel channel = new NotificationChannel(channelId , channelName, channelId);
// 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);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent notificationIntent = new Intent(context, NameSeachActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String s = bundle.getString(JPushInterface.EXTRA_EXTRA);
Intent intent0= new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle(title)//设置通知栏标题
.setContentText(message)
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setSmallIcon(R.mipmap.new_app_icon)//设置通知小ICON
.setChannelId(channelId)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pi);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
if (notificationManager != null) {
notificationManager.notify(notificationId , notification);
}
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
//跳转
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "极光推送出错:" + e.getMessage());
}
Android O系统会根据你的渠道类别,显示不同的通知需求,用户可以选择关闭某渠道的通知,并设置优先级
如上图所示:
该应用有3个渠道
附上判断8.0是否开启权限通知的方法:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Boolean notificationManager.areNotificationsEnabled();
到此就全部解决了。。。
大家有什么高见,可以留言哦。