OXYGENHUB
◆ v0.1.8 oxygenhub-android / app / src / main / java / io / oxygenhub / android / alerts / AlertService.kt
1.3 KiB · last changed Alerts from the hub: the phone registers its Cloud Messaging token and an X25519 key kept by the Keystore, and opens and shows what the hub seals to it with the Rust core; Firebase from google-services.json (the release job's firebase-config secret); pins OxygenFlow main (refs #16) 8 days ago source blame history raw
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
}