問題描述
在一些國內的一些機型與Android版本上,通知可能會出現不顯示的問題。
問題原因
使用了Notification.Builder構建通知,這個方法可能8.0(可能包含8.0)以下的版本無法正常創建通知了
解決問題
參考:https://www.jianshu.com/p/cb8426620e74
使用兼容模式的通知 NotificationCompat.Builder,進行創建。
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notify = null; int code = Integer.parseInt(RandomUtils.getRandomString(5)); PendingIntent pendingIntent3 = PendingIntent.getActivity(context.getApplicationContext(), code, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext()) .setSmallIcon(R.mipmap.app_icon) .setContentTitle(messageBean.getTitle()) .setContentText(messageBean.getReason()) .setContentIntent(pendingIntent3); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("to-do" , "待辦消息", NotificationManager.IMPORTANCE_HIGH); channel.enableVibration(true); channel.setVibrationPattern(new long[]{500}); manager.createNotificationChannel(channel); builder.setChannelId("to-do"); notify = builder.build(); } else { notify = builder.build(); } //使用默認的聲音 notify.defaults |= Notification.DEFAULT_SOUND; notify.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.doorbell); notify.defaults |= Notification.DEFAULT_VIBRATE; notify.flags |= Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失 manager.notify(code, notify);// 步驟4:通過通知管理器來發起通知。如果id不同,則每click,在status哪里增加一個提示 }