Files
seaca/app/src/main/java/ru/kalinamall/seaca/push/SeacaMessagingService.kt
T
PTah f41a0d3a50 feat: настройка звука push-уведомлений (0.5.4)
Единый звук для каналов, выбор системный/свой в Устройстве, исправлен краш ringtone picker.
2026-06-14 18:29:21 +10:00

58 lines
2.0 KiB
Kotlin

package ru.kalinamall.seaca.push
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import ru.kalinamall.seaca.data.NotificationMode
import ru.kalinamall.seaca.data.SacRepository
class SeacaMessagingService : FirebaseMessagingService() {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val repo by lazy { SacRepository(applicationContext) }
override fun onMessageReceived(message: RemoteMessage) {
val mode = runBlocking { repo.session.getNotificationMode() }
when (mode) {
NotificationMode.OFF -> return
NotificationMode.PUSH_SILENT -> show(message, silent = true)
NotificationMode.PUSH_SOUND -> show(message, silent = false)
}
}
private fun show(message: RemoteMessage, silent: Boolean) {
val data = message.data
val title = data["title"] ?: message.notification?.title ?: "SAC"
val body = data["body"] ?: message.notification?.body ?: ""
val soundUri = if (silent) {
NotificationSoundResolver.defaultNotificationUri()
} else {
val settings = runBlocking { repo.session.getNotificationSoundSettings() }
NotificationSoundResolver.resolve(this, settings)
}
NotificationHelper.show(
context = this,
title = title,
body = body,
severity = data["severity"],
kind = data["kind"],
id = data["id"],
silent = silent,
soundUri = soundUri,
)
}
override fun onNewToken(token: String) {
scope.launch {
runCatching {
if (repo.session.getAccessToken() != null) {
repo.registerFcmToken(token)
}
}
}
}
}