在Android 13中,通知的下一页和上一页按钮已被隐藏。要显示这些按钮,需要添加以下代码:
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.S){
Notification.BubbleMetadata bubbleMetadata = null;
Notification.Builder builder = new Notification.Builder(context, channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setBubbleMetadata(bubbleMetadata);
Notification.Action prevAction = new Notification.Action.Builder(
Icon.createWithResource(context, R.drawable.ic_previous),
"Previous",
prevPendingIntent
).build();
Notification.Action nextAction = new Notification.Action.Builder(
Icon.createWithResource(context, R.drawable.ic_next),
"Next",
nextPendingIntent
).build();
builder.addAction(prevAction);
builder.addAction(nextAction);
notificationManager.notify(notificationId, builder.build());
}
else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.ic_launcher_foreground);
NotificationCompat.Action prevAction = new NotificationCompat.Action.Builder(
R.drawable.ic_previous,
"Previous",
prevPendingIntent
).build();
NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder(
R.drawable.ic_next,
"Next",
nextPendingIntent
).build();
builder.addAction(prevAction);
builder.addAction(nextAction);
notificationManager.notify(notificationId, builder.build());
}
上面的代码进行了检查,如果设备API级别为Android 13或更高级别,则使用Notification.BubbleMetadata
和Notification.Action.Builder
添加下一页和上一页按钮。否则,使用NotificationCompat.Action.Builder
添加按钮。
值得注意的是,添加下一页和上一页按钮将使通知显示为气泡。如果您不想要气泡,可以将Notification.BubbleMetadata
和setBubbleMetadata()
删除,并将builder
替换为NotificationCompat.Builder
。