Current section

Files

Jump to
mob_new priv templates mob.new android app src main java MobFirebaseService.kt.eex
Raw

priv/templates/mob.new/android/app/src/main/java/MobFirebaseService.kt.eex

package <%= bundle_id %>
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.json.JSONObject
// Receives FCM messages and token refreshes.
// Token refreshes are cached and delivered to Elixir the next time a screen
// calls MobNotify.register_push/1 (mob_notify plugin).
// Foreground data pushes are forwarded immediately to the registered screen.
class MobFirebaseService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
io.mob.plugin.MobNotifyHub.pendingToken = token
}
// Called when a data message arrives while the app is in the foreground,
// or when any data-only message arrives regardless of app state.
// If the FCM payload includes mob_notification_json, that JSON is forwarded
// directly; otherwise a JSON object is built from the notification fields.
override fun onMessageReceived(message: RemoteMessage) {
val pid = io.mob.plugin.MobNotifyHub.notifyPid
if (pid == 0L) return
val json = message.data["mob_notification_json"] ?: run {
val notif = message.notification ?: return
JSONObject().apply {
put("title", notif.title ?: "")
put("body", notif.body ?: "")
put("source", "push")
put("data", JSONObject())
}.toString()
}
MobBridge.nativeDeliverNotification(pid, json)
}
// pendingToken moved to the generated io.mob.plugin.MobNotifyHub — the
// mob_notify plugin bridge drains it (this class can't be referenced from
// the plugin's package, and vice versa).
}