我正在开发音乐应用程序,因此我需要在通知栏上显示通知。为此,我正在使用自定义通知。以下是Android Oreo通知的代码
String NOTIFICATION_CHANNEL_ID = "001";
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.mynotification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription("Channel description");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.exo_edit_mode_logo)
.setCustomContentView(notificationLayout)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT);
NotificationManagerCompat com = NotificationManagerCompat.from(this);
com.notify(001, notificationBuilder.build());
}
此代码工作正常,但是从通知栏滑动通知时,它将被删除。但是我的要求不是在滑动通知时删除。 我尝试使用 setAutoCancel(true)和 setAutoCancel(false)。但是不起作用,它将被删除。那么如何在此处粘贴通知。
请指导我该怎么做。
预先感谢
答案 0 :(得分:1)
您必须添加此setOngoing(true)
尝试:
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText("Text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(false)
.setOngoing(true);
Notification notification = builder.build();
答案 1 :(得分:1)
使用setOngoing(true)
实现这一目标