在Android O上禁用通知声音 [英] Disable notification sound on Android O
问题描述
我尝试通过以下方法摆脱通知声音.
我能够将其减少到只能关闭一次,但它应该在Android O和更低版本中完全保持静音.
我在stackoverflow和google上搜索了很长时间,但直到现在都没有完全起作用.
感谢您的帮助.
public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
(NOTIFICATION_CHANNEL_ID, "Test Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setSound(null, null);
// Configure the notification channel.
notificationChannel.setDescription("Channel test");
notificationManager.createNotificationChannel(notificationChannel);
}
Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(appName)
.setContentText(status)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setDefaults(0)
.setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setProgress(100, progress, progress == 0)
.setWhen(downloadStart)
.setContentIntent(cancel)
.setGroup(GROUP_KEY)
.addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
.setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color))
.setOnlyAlertOnce(true);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.addLine(status);
notification.setStyle(inboxStyle);
notificationManager.notify(id, notification.build());
addNotification(id);
}
经过更多研究,我发现了这一点.
这是至关重要的部分:
//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null); <---- ignore sound
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);
起初,它仍然无法正常运行,但在卸载我的应用程序并重新安装后,一切都很好.
如果有人需要,这是正确的代码:
public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW
);
//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null);
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(appName)
.setContentText(status)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setDefaults(0)
.setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setProgress(100, progress, progress == 0)
.setWhen(downloadStart)
.setContentIntent(cancel)
.setGroup(GROUP_KEY)
.addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
.setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color));
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.addLine(status);
notification.setStyle(inboxStyle);
notificationManager.notify(id, notification.build());
addNotification(id);
}
I try to get rid of the notification sound in below method.
I was able to reduced it to only go off once but it should be completely silent in Android O and lower versions.
I searched a long time on stackoverflow and google but till now nothing completely works.
Any help is appreciated.
public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
(NOTIFICATION_CHANNEL_ID, "Test Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setSound(null, null);
// Configure the notification channel.
notificationChannel.setDescription("Channel test");
notificationManager.createNotificationChannel(notificationChannel);
}
Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(appName)
.setContentText(status)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setDefaults(0)
.setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setProgress(100, progress, progress == 0)
.setWhen(downloadStart)
.setContentIntent(cancel)
.setGroup(GROUP_KEY)
.addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
.setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color))
.setOnlyAlertOnce(true);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.addLine(status);
notification.setStyle(inboxStyle);
notificationManager.notify(id, notification.build());
addNotification(id);
}
I figured it out after some more research.
This is the vital part:
//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null); <---- ignore sound
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);
At first on implementing this, it still did not work but after uninstalling my app, and reinstall it everything was fine.
If any one needs it, here is the correct code:
public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW
);
//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null);
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(appName)
.setContentText(status)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setDefaults(0)
.setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setProgress(100, progress, progress == 0)
.setWhen(downloadStart)
.setContentIntent(cancel)
.setGroup(GROUP_KEY)
.addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
.setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color));
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.addLine(status);
notification.setStyle(inboxStyle);
notificationManager.notify(id, notification.build());
addNotification(id);
}
这篇关于在Android O上禁用通知声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!