如何强制通知使用警报声音而不是铃声?

时间:2020-04-17 06:29:43

标签: kotlin notifications alarm ringtone

我的问题:我已将通知设置为警报。当手机上的声音打开时,手机会将声音作为警报声音。但是,如果手机静音,则手机会振动,但没有声音/没有警报声/没有铃声。我什至看不到警报的音量/声音级别,只有铃声的音量在手机上可见。警报在不同的模拟器上都可以正常工作->所以问题是:有没有办法强制通知使用闹铃而不是铃声?一个想法将不胜感激。(PS:我也尝试将其作为通知服务)谢谢!这是我的代码:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channelImportance = if (playSound) {
            NotificationManager.IMPORTANCE_HIGH
        } else {
            NotificationManager.IMPORTANCE_LOW
        }
        val audioAttribute = AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build()
        val notificationSound: Uri =
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
        val notyChannel =
            NotificationChannel(channelID, channelName, channelImportance)

        notyChannel.enableLights(true)
        notyChannel.lightColor = Color.RED
        notyChannel.setSound(notificationSound,audioAttribute)
        notyChannel.enableVibration(true)
        notyChannel.vibrationPattern = longArrayOf(
            1000,100,100,100,1000,100,100,100,1000)

        this.createNotificationChannel(notyChannel)

1 个答案:

答案 0 :(得分:0)

this works:
val audio =
            context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        if (mode == 0) audio.ringerMode =
            AudioManager.RINGER_MODE_SILENT else if (mode == 1) audio.ringerMode =
            AudioManager.RINGER_MODE_VIBRATE else if (mode == 2) audio.ringerMode =
            AudioManager.RINGER_MODE_NORMAL