可以使用 NotificationChannel 类将通知分类,并设置通知通道的重要性等级为“低”,即可将其归类为静音通知。
示例代码如下:
// 创建一个通知渠道,设置名称和重要性等级
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_LOW);
// 将通知归类为静音通知
channel.setCategory(NotificationCompat.CATEGORY_MESSAGE);
// 注册通知渠道
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
// 创建通知并发送
Notification notification = new NotificationCompat.Builder(context, "channel_id")
.setContentTitle("Title of notification")
.setContentText("Content of notification")
.setSmallIcon(R.drawable.notification_icon)
.build();
notificationManager.notify(notificationId, notification);