安卓通知渠道总是默认为静音

4

我正在创建Android通知渠道,希望通知能够震动并播放声音。但出于某种原因,通知始终显示在安卓下拉菜单的“静音”组中。没有播放任何声音或震动。这是我使用的代码:

val channelId = getString(R.string.default_notification_channel_id)
        val channelName = getString(R.string.default_notification_channel_name)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val bigTextStyle = NotificationCompat.BigTextStyle()
            .bigText(messageBody)
            .setBigContentTitle(messageTitle)

        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_icon)
            .setColor(getColor(R.color.deeper_blue))
            .setStyle(bigTextStyle)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pendingIntent)

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            

            val channel = NotificationChannel(
                channelId,
                channelName,
                NotificationManager.IMPORTANCE_HIGH
            )
            val att = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build()
            channel.enableLights(true)
            channel.lightColor = getColor(R.color.deeper_blue)
            channel.enableVibration(true)
            // Vibration Pattern if vibration is enabled
            val VIBRATION_DURATION = 1000L
            val WAITING_DURATION = 2000L
            channel.vibrationPattern = longArrayOf(WAITING_DURATION, VIBRATION_DURATION, WAITING_DURATION, VIBRATION_DURATION)
            channel.setSound(defaultSoundUri,att)
            notificationManager.createNotificationChannel(channel)
        }
        val rnds = (0..1000).random()
        notificationManager.notify((rnds * System.currentTimeMillis()).toInt() /* ID of notification */, notificationBuilder.build())

我正在测试运行Android 11的Google Pixel 4a 5G。以下是来自Android设置的屏幕截图,这是默认创建的通道。 enter image description here 如果我切换到默认,它将开始发出警报和振动,但在安装应用程序时,它不会自动设置为默认

请尝试使用此链接验证通知代码与您的代码是否匹配,并尝试使用它,可能会对您有所帮助:https://stackoverflow.com/a/68487811/4042384 - gpuser
3个回答

1
首先,您需要更改为NotificationManager.IMPORTANCE_HIGH,然后在编辑通知设置时,如果有频道通知ID,则必须重命名CHANNEL ID,因为该频道只能被创建而不能被修改!例如,我们想要在版本1应用程序中打开通知声音,并且频道ID为1,然后我们想要在版本2应用程序中关闭通知声音,我们必须重命名频道ID,以便通知会发出声音。请参见:禁用NotificationChannel的声音,请参见:https://dev59.com/zVcO5IYBdhLWcg3wXgfh#47144981

1
关于震动,您是否已经添加了权限?
请在您的Manifest.xml中添加以下内容: <uses-permission android:name="android.permission.VIBRATE" />

是的,我在清单中添加了权限。如果我进入Android设置>通知>我的应用程序>通知,我可以从静音更改为默认,它会开始响铃。 - Easy Coder

0

我在Android 11上遇到了同样的问题。我尝试更改通知设置,结果将通道中的NotificationManager.IMPORTANCE_MAX更改为NotificationManager.IMPORTANCE_HIGH后,问题得到解决。我不知道为什么会出现这种情况,但之前使用NotificationManager.IMPORTANCE_MAX时一切正常。我猜测这是一个Android的bug。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接