java - 如何在没有声音的情况下显示通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39809702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show a notification without a sound java
提问by Jason
How can I make a notification that doesn't make a sound when I build it? I am building a notification, and my users don't like the fact that it makes a sound.
如何在构建时发出不发出声音的通知?我正在构建一个通知,我的用户不喜欢它发出声音的事实。
How can I change it to a silent one / no sound at all?
如何将其更改为静音/根本没有声音?
How I show notification:
我如何显示通知:
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.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());
I tried to search on google, but the only results I get is HOW to play a sound, not HOW to not play a sound...
我试图在谷歌上搜索,但我得到的唯一结果是如何播放声音,而不是如何不播放声音......
EditIt possibly is a duplicate in some people's eyes, but in mine I could not find out an alternative for the there specified default, while this new method is called setDefaults
编辑它在某些人看来可能是重复的,但在我看来,我找不到指定默认值的替代方法,而这个新方法称为 setDefaults
采纳答案by Nongthonbam Tonthoi
Remove the line to builder.setDefaults(Notification.DEFAULT_ALL);
. It will not play the sound, but you may need to enable all other notification defaults if preferred
删除到 的行builder.setDefaults(Notification.DEFAULT_ALL);
。它不会播放声音,但如果愿意,您可能需要启用所有其他通知默认值
回答by albeee
Use this If you want all (sound, vibration and lights) in notifications.
如果您想要通知中的所有(声音、振动和灯光),请使用此选项。
builder.setDefaults(Notification.DEFAULT_ALL);
Or you can enable or disable items based on your requirements.
或者您可以根据您的要求启用或禁用项目。
builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
comment this line if you want nothing.
如果你什么都不想要,请评论这一行。
回答by user1540907
Easy way to go to be able to show notification with any kind of priority is to set some sound that is silence actually. Just generate some silence.mp3 put it in "raw" folder and set notification sounds using Uri:
能够以任何优先级显示通知的简单方法是设置一些实际上是静音的声音。只需生成一些silence.mp3 将其放入“raw”文件夹并使用Uri 设置通知声音:
Uri.parse("android.resource://"+YOUR_APP_PACKAGE_NAME+"/"+R.raw.silence)
You can generate this .mp3 with app like Audacity. It has option generate silence, just set how many seconds and you are good to go.
您可以使用 Audacity 等应用程序生成此 .mp3。它有选项生成静音,只需设置多少秒就可以了。
If you set defaults to 0 and set sound to null, notification will be shown without you hearing it but you wont be able to show notifications with some higher priority.
如果您将默认值设置为 0 并将声音设置为空,通知将在您听不到的情况下显示,但您将无法显示具有更高优先级的通知。
回答by Ignacio Tomas Crespo
In android O, for me it worked with this settings in the notification:
在 android O 中,对我来说,它在通知中使用了以下设置:
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
.setGroup("My group")
.setGroupSummary(false)
.setDefaults(NotificationCompat.DEFAULT_ALL)
回答by Kuldip Sharma
To disable the sound in OREO 8.1, change the priority of the notification as LOW and it will disable the sound of notification:
要在 OREO 8.1 中禁用声音,请将通知的优先级更改为 LOW,它将禁用通知声音:
NotificationManager.IMPORTANCE_LOW
The code is like:
代码是这样的:
NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);
回答by Михаил
It works for me in Android Oreo.
它在 Android Oreo 中对我有用。
You should just write your channel like this:
你应该像这样写你的频道:
NotificationChannel notificationChannel = new NotificationChannel("Id" , "Name", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(null, null);
notificationChannel.setShowBadge(false);
notificationManager.createNotificationChannel(notificationChannel);
回答by Ramy Bakkar
use that exact code:
使用那个确切的代码:
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
NOTIFICATION_CHANNEL_ID --> random String.
channelName ==> random string
回答by rana
I might be late but still wants to add this . You can disable sound using .setSound(null)
on NotificationCompat.Builder builder
for all OS below O.
我可能会迟到但仍然想添加这个。您可以.setSound(null)
对NotificationCompat.Builder builder
O 以下的所有操作系统禁用声音。
For O version n above add channel.setSound(null,null)
after creating NotificationChannel channel
对于上面的 O 版本 nchannel.setSound(null,null)
在创建后添加NotificationChannel channel
All the solutions above mine is either outdated or covers some OS versions only
我上面的所有解决方案都已过时或仅涵盖某些操作系统版本
回答by Carlos Gómez
You can set more than one channel. In my case, my aplication has a background service with two sounds notifications, and one notification without sound. I get it with this code:
您可以设置多个频道。就我而言,我的应用程序有一个后台服务,有两个声音通知,一个没有声音通知。我用这个代码得到它:
//creating channels:
//创建渠道:
NotificationChannel channel1 = new NotificationChannel(CHANNEL_ID_1, "CarNotification1", NotificationManager.IMPORTANCE_HIGH);
NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_2, "CarNotification2", NotificationManager.IMPORTANCE_MIN);
NotificationChannel channel3 = new NotificationChannel(CHANNEL_ID_3, "CarNotification3", NotificationManager.IMPORTANCE_HIGH);
//mSound1 and mSound2 are Uri
channel1.setSound(mSound1, null);
channel3.setSound(mSound2, null);
and when I create the notification:
当我创建通知时:
String channelId;
switch (situation){
case situation2:
channelId=CHANNEL_ID_2;
break;
case situation1:
channelId=CHANNEL_ID_1;
break;
default:
channelId=CHANNEL_ID_3;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
//etcetera