简体   繁体   English

在 Android 通知中禁用声音(不改变视觉行为)

[英]Disable sound in Android Notifications (without changing visual behavior)

I was able to remove sound with IMPORTANCE_LOW creating the channel, but notification was not showing up on the screen.我能够通过 IMPORTANCE_LOW 创建频道来删除声音,但通知没有显示在屏幕上。 I want notification to show itself like in IMPORTANCE_HIGH but with no sound.我希望通知像 IMPORTANCE_HIGH 一样显示,但没有声音。 One probably possible hacky way to do that is creating a silent sound mp3 in raw folder and use it in notification.一种可能可行的方法是在原始文件夹中创建无声的 mp3 并在通知中使用它。

Is there a proper way to only disable sound in Android Notifications without changing the visual behavior of notification itself?有没有一种正确的方法可以只在 Android 通知中禁用声音而不改变通知本身的视觉行为? I am looking for a solution for Android versions between 4.4 and 11我正在寻找适用于 4.4 和 11 之间的 Android 版本的解决方案

Relevant piece of code of what I've tried so far:到目前为止我尝试过的相关代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(getString(R.string.channel_id), getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH)
            channel.enableVibration(true)
            channel.setSound(null, null)
            mNotificationManager.createNotificationChannel(channel)
}

var builder = NotificationCompat.Builder(this, chId)
            .setSmallIcon(R.drawable.notification_alarm)
            .setContentTitle(getString(R.string.tx_title))
            .setContentText(message)
            .setAutoCancel(true)
            .setTicker(message)

builder.setDefaults(0)
//builder.setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_VIBRATE)
startForeground(ENotifications.id, builder.build())

put this code to your project's将此代码放入您的项目的

 NotificationChannel notificationChannel = new NotificationChannel("IdNotif","NameNotif", 
 NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setSound(null, null);
        notificationChannel.setShowBadge(false);
        notificationManager.createNotificationChannel(notificationChannel);

i use notificationChannel.setSound(null, null);我使用 notificationChannel.setSound(null, null); to disable sound and no use blank sound.禁用声音,不使用空白声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM