Coverage / app/src/main/java/io/oxygenhub/android/alerts/AlertService.kt 0.0%
0 of 12 lines run, 0 of 4 functions, 0 of 8 branches · run #1635 on main · run missed
| 1 | package io.oxygenhub.android.alerts | |
| 2 | ||
| 3 | import android.util.Log | |
| 4 | import com.google.firebase.messaging.FirebaseMessagingService | |
| 5 | import com.google.firebase.messaging.RemoteMessage | |
| 6 | import io.oxygenhub.android.OxygenHubApp | |
| 7 | import kotlinx.coroutines.CoroutineScope | |
| 8 | import kotlinx.coroutines.Dispatchers | |
| 9 | import kotlinx.coroutines.SupervisorJob | |
| 10 | import kotlinx.coroutines.launch | |
| 11 | ||
| 12 | /** Cloud Messaging's side of the hub's alerts (#16): a new token, or a sealed alert. */ | |
| 13 | class AlertService : FirebaseMessagingService() { | |
| 14 | private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) | |
| 15 | ||
| 16 | /** The phone's registration changed: tell the hub, when signed in. */ | |
| 17 | override fun onNewToken(token: String) { | |
| 18 | val api = (application as OxygenHubApp).container.sessions.api() ?: return | |
| 19 | scope.launch { | |
| 20 | runCatching { PushAlerts.register(applicationContext, api, token) } | |
| 21 | .onFailure { Log.w("AlertService", "registering with the hub: ${it.message}") } | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | /** Only data messages come: open the sealed alert and show it. */ | |
| 26 | override fun onMessageReceived(message: RemoteMessage) { | |
| 27 | val sealed = message.data["sealed"] ?: return | |
| 28 | val alert = PushAlerts.open(applicationContext, sealed) ?: return | |
| 29 | PushAlerts.show(applicationContext, alert) | |
| 30 | } | |
| 31 | } |