当应用程序完全关闭(被杀死)时,Android Oreo无法在状态栏中和通知本身上正确显示通知图标。当在前台打开应用程序时,它运行良好。
int notificationId = new Random().nextInt(60000);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, notificationId + 1,
intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupChannels();
}
String CHANNEL_ID = getString(R.string.default_notification_channel_id);
Bitmap bitmap = null;
if (imageUrl != null && !imageUrl.equalsIgnoreCase("")) {
bitmap = getBitmapfromUrl(imageUrl);
}
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx, CHANNEL_ID)
.setContentTitle(notificationTitle)
.setStyle(new NotificationCompat.BigPictureStyle()
.setSummaryText(notificationMsg)
.bigPicture(bitmap))/*Notification with Image*/
.setSound(defaultSoundUri)
.setContentText(msg)
.setChannelId(CHANNEL_ID)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_HIGH);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setColor(getResources().getColor(R.color.colorPrimaryDark));
}
mBuilder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), getNotificationIcon()));
mBuilder.setSmallIcon(getNotificationIcon());
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O);
return useWhiteIcon ? R.mipmap.ic_launcher_icon_round : R.mipmap.ic_launcher;
}
预先感谢...