我想显示这样的通知(在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,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,1)
.setPriority(Notificationmanager.IMPORTANCE_HIGH)
.setContentIntent(notifyPendingIntent);
notificationmanager.notify("do_not1",notificationBuilder.build());
}
我不知道为什么它在较低的api上不起作用,但是在较高的api上却不起作用!
NotificationChannel似乎有问题,但无法找到原因。
如何通过OREO api显示如上图所示的通知的完整内容?