elpaso
elpaso

Reputation: 21

How to force the notification to use the Alarmsound and not the Ringtone?

My Problem: i have set the notification as an alarm.When the sound on the cellphone is on, the cellphone takes the sound as an alarmsound. But if the cellphone is muted, the cellphone vibrates, but there is no sound./no alarmsound/no ringtone. I cant even see the volume/sound level for the alarm, only the volume of the ringtone is visible on the cellphone.The alarm works fine on different emulator-> So the question is: Is there a way to force the notification to use the Alarmsound and not the Ringtone? An idea would be appreciated.(PS: i have tried it also as a service with notification)Thank you!here is my my code:

 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)

Upvotes: 0

Views: 202

Answers (1)

elpaso
elpaso

Reputation: 21

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

Upvotes: 0

Related Questions