【发布时间】:2018-01-25 22:50:18
【问题描述】:
我尝试消除以下方法中的通知声音。
我能够将它减少到只关闭一次,但它应该在 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);
}
【问题讨论】:
-
您需要卸载应用程序并重新安装它,因为一旦使用声音创建通知通道,它将不会被覆盖。因此,您需要卸载该应用并重新安装。
标签: android notifications android-8.0-oreo