如何在 Android Oreo 及更高版本的通知托盘中显示带图片的通知?
How to show Android notification with picture in notification tray in Android Oreo and above?
我想显示这样的通知(在 Android 7.0 API24 上):
但我得到的结果只有托盘图标而不是全部内容。(Android 8.1.0 API27):
代码:
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelID="channelID";
String channelName = "channelName";
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Android 8.1.0 API27
NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, notificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelID);
notificationBuilder
.setSmallIcon(R.drawable.ic_add_post)
.setContentTitle("some title")
.setContentText("some message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000, 1000})
.setLights(Color.BLUE, 1,1)
.setShowWhen(true)
.setContentIntent(notifyPendingIntent);
notificationManager.notify("do_not1",0 /* ID of notification */, notificationBuilder.build());
} else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder
.setSmallIcon(R.drawable.ic_add_post)
.setContentTitle("some title")
.setContentText("some message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000, 1000})
.setLights(Color.BLUE, 1,1)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setContentIntent(notifyPendingIntent);
notificationManager.notify("do_not1",0 /* ID of notification */, notificationBuilder.build());
}
我不知道为什么它在较低 api 下工作,但在较高 api 下它不起作用!
NotificationChannel 好像有问题,但找不到原因。
如何在over OREO api显示通知的全部内容如上图?
我在您的代码中没有发现任何可疑之处,但有可能 Android 删除了此功能(重要通知的先睹为快)- 对此一无所知 - 或自定义 OEM制作人重新设计了它 UI 认为这种先睹为快的做法是不受欢迎的
我想显示这样的通知(在 Android 7.0 API24 上):
但我得到的结果只有托盘图标而不是全部内容。(Android 8.1.0 API27):
代码:
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelID="channelID";
String channelName = "channelName";
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Android 8.1.0 API27
NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, notificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelID);
notificationBuilder
.setSmallIcon(R.drawable.ic_add_post)
.setContentTitle("some title")
.setContentText("some message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000, 1000})
.setLights(Color.BLUE, 1,1)
.setShowWhen(true)
.setContentIntent(notifyPendingIntent);
notificationManager.notify("do_not1",0 /* ID of notification */, notificationBuilder.build());
} else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder
.setSmallIcon(R.drawable.ic_add_post)
.setContentTitle("some title")
.setContentText("some message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000, 1000})
.setLights(Color.BLUE, 1,1)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setContentIntent(notifyPendingIntent);
notificationManager.notify("do_not1",0 /* ID of notification */, notificationBuilder.build());
}
我不知道为什么它在较低 api 下工作,但在较高 api 下它不起作用!
NotificationChannel 好像有问题,但找不到原因。
如何在over OREO api显示通知的全部内容如上图?
我在您的代码中没有发现任何可疑之处,但有可能 Android 删除了此功能(重要通知的先睹为快)- 对此一无所知 - 或自定义 OEM制作人重新设计了它 UI 认为这种先睹为快的做法是不受欢迎的