Allow notifications to use the alarm stream (#1019)

* Allow notifications to use the alarm stream

* Review comment
This commit is contained in:
Daniel Shokouhi 2020-10-08 08:20:41 -07:00 committed by GitHub
parent 6975acece9
commit 3c0f1a8dce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
package io.homeassistant.companion.android.notifications
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
@ -7,6 +8,8 @@ import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.RingtoneManager
import android.os.Build
import android.speech.tts.TextToSpeech
@ -206,7 +209,8 @@ class MessagingService : FirebaseMessagingService() {
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
handleSound(notificationBuilder, data)
handlePersistent(notificationBuilder, tag, data)
@ -329,6 +333,18 @@ class MessagingService : FirebaseMessagingService() {
return groupNotificationBuilder
}
private fun handleSound(
builder: NotificationCompat.Builder,
data: Map<String, String>
) {
if (data["channel"] == "alarm_stream") {
builder.setCategory(Notification.CATEGORY_ALARM)
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM), AudioManager.STREAM_ALARM)
} else {
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
}
}
private fun handleColor(
builder: NotificationCompat.Builder,
data: Map<String, String>
@ -595,6 +611,9 @@ class MessagingService : FirebaseMessagingService() {
handleImportance(data)
)
if (channelName == "alarm_stream")
handleChannelSound(channel)
setChannelLedColor(data, channel)
setChannelVibrationPattern(data, channel)
notificationManagerCompat.createNotificationChannel(channel)
@ -628,6 +647,17 @@ class MessagingService : FirebaseMessagingService() {
}
}
@RequiresApi(Build.VERSION_CODES.O)
private fun handleChannelSound(
channel: NotificationChannel
) {
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM), audioAttributes)
}
private fun parseVibrationPattern(
vibrationPattern: String?
): LongArray {