我的通知仍然发出声音。我做错了什么?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// For foreground service
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Creating channel for notification
String id = BackroundService.class.getSimpleName();
String name = BackroundService.class.getSimpleName();
NotificationChannel notificationChannel = new NotificationChannel(id,
name, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(notificationChannel);
notificationChannel.setSound(null, null);
// Foreground notification
Notification notification = new Notification.Builder(this, id)
.setContentTitle(getText(R.string.app_name))
.setContentText("KSL is protecting you!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setTicker("Ticker text")
.build();
startForeground(9, notification);
}
我使用了notificationChannel.setSound(null, null);
方法,但是它不起作用。
答案 0 :(得分:0)
在您的通知中提供您的频道ID。
Notification notification = new Notification.Builder(this, id)
.setContentTitle(getText(R.string.app_name))
.setContentText("KSL is protecting you!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setTicker("Ticker text")
.setChannelId(channelId)
.build();
修改
确保卸载并重新安装您的应用,以使更改生效。
编辑2
NotificationChannel notificationChannel = new NotificationChannel(id,
name, NotificationManager.IMPORTANCE_LOW);
Android系统将通知重要性设置为 NotificationManager.IMPORTANCE_HIGH (默认情况下会播放声音),即使您提供了 NotificationManager.IMPORTANCE_DEFAULT 的重要性(此设置也不会播放声音)默认)。我不知道为什么会这样,但是为了解决您的问题,我建议将重要信息更改为 NotificationManager.IMPORTANCE_LOW 。