如何在没有声音的情况下显示通知java

新手上路,请多包涵

如何在构建时发出不发出声音的通知?我正在构建一个通知,但我的用户不喜欢它发出声音这一事实。

我怎样才能把它改成静音/完全没有声音?

我如何显示通知:

 android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(main);
builder.setStyle(new android.support.v7.app.NotificationCompat.BigTextStyle().bigText(text));
builder.setSmallIcon(R.drawable.app);
builder.setContentTitle("Rooster Maandag:");
builder.setOngoing(false);
builder.setAutoCancel(true);
builder.setSilent(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager = (NotificationManager) main.getSystemService(main.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

我试图在谷歌上搜索,但我得到的唯一结果是如何播放声音,而不是如何不播放声音……

编辑 在某些人看来它可能是重复的,但在我看来我找不到指定默认值的替代方法,而这个新方法称为 setDefaults

原文由 Jason 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 464
2 个回答

删除到 builder.setDefaults(Notification.DEFAULT_ALL); 的行。它不会播放声音,但如果愿意,您可能需要启用所有其他通知默认设置

原文由 Nongthonbam Tonthoi 发布,翻译遵循 CC BY-SA 3.0 许可协议

要在 OREO 8.1 中禁用声音,将通知的优先级更改为 LOW,它将禁用通知声音:

 NotificationManager.IMPORTANCE_LOW

代码是这样的:

 NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);

原文由 Kuldip Sharma 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题