Skip to content

Commit

Permalink
Allow notifications to use the alarm stream (#1019)
Browse files Browse the repository at this point in the history
* Allow notifications to use the alarm stream

* Review comment
  • Loading branch information
dshokouhi committed Oct 8, 2020
1 parent 6975ace commit 3c0f1a8
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.homeassistant.companion.android.notifications

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -595,6 +611,9 @@ class MessagingService : FirebaseMessagingService() {
handleImportance(data)
)

if (channelName == "alarm_stream")
handleChannelSound(channel)

setChannelLedColor(data, channel)
setChannelVibrationPattern(data, channel)
notificationManagerCompat.createNotificationChannel(channel)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3c0f1a8

Please sign in to comment.