Dan*_*l F 71 notifications android
今天我开始针对API 26,这迫使我使用通知频道.
我的问题是,现在每个新通知(包括对它的更新)都会播放恼人的声音.
如何禁用此声音?
我尝试用自定义的mp3声音替换这个声音,然后在其中传递一个静音的mp3,但这被忽略了.
我只是添加一个优先级非常低的通知,基本上让用户可以选择在与应用程序交互后执行某些操作.没有理由大声说话,用户会知道他可以参考通知,因为他已经对应用程序做了一些他知道会导致通知出现的事情.
用户真的会开始对这种声音感到恼火.
mVc*_*Vck 99
如果你想保持你的频道的重要性,只是删除声音notificationChannel.setSound(null, null);
似乎做的工作.
编辑: 确保重命名频道(并删除旧频道)以将其应用于现有用户.(可以创建频道,但不会被应用修改,只有用户才能修改.)
Dan*_*l F 84
解决方案是使用NotificationManager.IMPORTANCE_LOW
并为其创建新渠道.创建频道后,您无法更改重要性(嗯,您可以,但忽略新的重要性).频道信息似乎由系统永久存储,并且只有在您卸载应用程序时才会删除创建的任何频道.[ 更新:根据Ferran Negre的评论,您可以通过删除频道NotificationManager.IMPORTANCE_LOW
并重新创建nm.deleteNotificationChannel(nChannel.getId());
]
虽然以前的Android版本默认没有播放声音,但是这已经改变了Android O,但只有当您定位API 26时,即使用通知频道.这是不一致的,实际上,这是一个错误:
这样做的原因是,当您创建一个频道nm.createNotificationChannel(nChannel);
(默认情况下不值得)时,Android实际上会"稍微"将其注册为NotificationManager.IMPORTANCE_DEFAULT
(默认情况下播放声音).
您可以通过进入通知选项(长按通知条目)来检查这一点,您将在其中读取它的类型NotificationManager.IMPORTANCE_HIGH
,然后禁用通知,然后重新启用它.在这个过程中,它被"降级"从不NotificationManager.IMPORTANCE_HIGH
发声,实际登记NotificationManager.IMPORTANCE_HIGH
.
该错误已提交给Android问题跟踪器,因此您可能想要将其加星标记(由Google标记为"不会修复(不可行)",因为...已被破坏).
顺便说一句,https://developer.android.com/training/notify-user/channels上的新文档 声称默认行为是这样的,默认情况下在Android 8.0之前播放声音,这绝对不是真的.这是他们的清单
User-visible importance level Importance Priority
(Android 8.0 and higher) (Android 7.1 and lower)
Urgent Makes a sound and appears as IMPORTANCE_HIGH PRIORITY_HIGH
a heads-up notification or PRIORITY_MAX
High Makes a sound IMPORTANCE_DEFAULT PRIORITY_DEFAULT
Medium No sound IMPORTANCE_LOW PRIORITY_LOW
Low No sound and does not appear IMPORTANCE_MIN PRIORITY_MIN
in the status bar
Run Code Online (Sandbox Code Playgroud)
你甚至可以看到能见度重要性高和通知重要性之间的不匹配......我不知道为什么他们这样做.他们的代码肯定有一个错误.
下一行下面的所有内容都已过时,但那里提到的bug仍然有效.我的错误是认为这NotificationManager.IMPORTANCE_DEFAULT
是下一个较低的NotificationManager.IMPORTANCE_MIN
,但是NotificationManager.IMPORTANCE_DEFAULT
.
然后,当您通过长按通知和全通道按钮进入应用程序的通知设置并关闭该通道的开关并再次打开时,它实际上将自己设置为NotificationManager.IMPORTANCE_LOW
并且不会播放任何声音.我也注意到在崩溃后它确实被重置为NotificationManager.IMPORTANCE_DEFAULT
所以基本上解决方法是使用NotificationManager.IMPORTANCE_HIGH
.但是您必须创建一个新频道才能使其NotificationManager.IMPORTANCE_MIN
生效,因为一旦您创建了频道,您就无法改变现有频道的重要性.
更新:原因是解决方法NotificationManager.IMPORTANCE_MIN
有一个缺点.
当您使用该重要性级别时,您的通知不再完全显示在通知抽屉内,而是将其自身插入到默认折叠的新通知通道组中(并且每次拉下抽屉时将再次折叠).太可惜了!
更新2:深入挖掘它发现它好像它正确地注册了它NotificationManager.IMPORTANCE_MIN
,但不知何故它神奇地升级到了NotificationManager.IMPORTANCE_DEFAULT
,就像当用户明确地将设置从默认更改为高时.在关闭通知然后再打开之后,也会将其重置为默认值.
NotificationCompat.Builder.setSilent(true)
这允许您发布无声通知(没有声音或振动),无论频道的重要性设置如何。
小智 7
据我所知,从 API 26 (Oreo) 开始,通知一旦创建就无法更改通知的声音。
notificationManager.deleteNotificationChannel("channel_id"));
NotificationChannel notificationChannel = new NotificationChannel(
"channel_id", "channel_name",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
Run Code Online (Sandbox Code Playgroud)
即使删除频道创建之前确实没有帮助。
谷歌文档说:
android.app.NotificationManager public void deleteNotificationChannel(String channelId)
删除给定的通知渠道。如果您创建一个具有相同 ID 的新频道,则删除的频道将被取消删除,其设置与删除前的所有设置相同。
NotificationChannel#setSound()
文件说明
只能在频道提交之前修改
NotificationManager#createNotificationChannel(NotificationChannel)
太糟糕了,这notificationBuilder.setSound(defaultSoundUri)
不起作用:
此方法在 API 级别 26 中已弃用。请改用 NotificationChannel#setSound(Uri, AudioAttributes)。
使用支持库也不起作用。所以声音只能在应用程序中设置一次,并且用户只能在通知的设置中进行更改。对我来说,Ferran Negre 的评论不起作用。我不明白谷歌为什么做出这个限制。太糟糕了。
好吧,我会添加一个完整的答案来提供帮助。如果您NotificationCompat
从androidx读取代码。
/**
* Silences this instance of the notification, regardless of the sounds or vibrations set
* on the notification or notification channel.
*/
public @NonNull Builder setNotificationSilent() {
mSilent = true;
return this;
}
Run Code Online (Sandbox Code Playgroud)
所以如果你想消除声音和振动,你必须像这样使用。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
// no sound or vibration
.setNotificationSilent()
Run Code Online (Sandbox Code Playgroud)
如果您只想删除声音。这就是方法。
// no sound
builder.setSound(null);
Run Code Online (Sandbox Code Playgroud)
如果您只想删除病毒。
// no vibration
mChannel.setVibrationPattern(new long[]{ 0 });
mChannel.enableVibration(true);
Run Code Online (Sandbox Code Playgroud)
我已经测试了很多 android 设备,以下代码适合我
首先创建一个notificationBuilder,如果你的Build.Version大于26,请添加一个新的频道。
private val notificationBuilder: NotificationCompat.Builder by lazy {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) NotificationCompat.Builder(context) else {
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = "MUSIC"
val channelName = "?????"
val importance = NotificationManager.IMPORTANCE_MIN
val channel = NotificationChannel(channelId, channelName, importance)
manager.createNotificationChannel(channel)
channel.enableLights(false)
channel.vibrationPattern = longArrayOf(0L)
channel.enableVibration(false)
channel.setSound(null, null)
NotificationCompat.Builder(context, channelId)
}
}
Run Code Online (Sandbox Code Playgroud)
其次,初始化这个notificationBuilder,并设置sound null
notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS ).setVibrate( longArrayOf(0L)).setSound(null)
Run Code Online (Sandbox Code Playgroud)
第三,如果 build.version 大于 24,请设置其优先级。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
notificationBuilder.priority = NotificationManager.IMPORTANCE_MIN
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有用。
归档时间: |
|
查看次数: |
28921 次 |
最近记录: |