From 042a7dc66bcec1a3593fe0e7a141916b37433c36 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Mon, 13 Dec 2021 22:00:48 -0500 Subject: [PATCH] Subscribe to control topic when keepalive message on non-control topic is received --- .../java/io/heckel/ntfy/firebase/FirebaseService.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt b/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt index 56c6235..61caac9 100644 --- a/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt +++ b/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt @@ -22,6 +22,7 @@ class FirebaseService : FirebaseMessagingService() { private val job = SupervisorJob() private val notifier = NotificationService(this) private val broadcaster = BroadcastService(this) + private val messenger = FirebaseMessenger() override fun onMessageReceived(remoteMessage: RemoteMessage) { // We only process data messages @@ -33,15 +34,20 @@ class FirebaseService : FirebaseMessagingService() { // Dispatch event val data = remoteMessage.data when (data["event"]) { - ApiService.EVENT_KEEPALIVE -> handleKeepalive() + ApiService.EVENT_KEEPALIVE -> handleKeepalive(remoteMessage) ApiService.EVENT_MESSAGE -> handleMessage(remoteMessage) else -> Log.d(TAG, "Discarding unexpected message (2): from=${remoteMessage.from}, data=${data}") } } - private fun handleKeepalive() { + private fun handleKeepalive(remoteMessage: RemoteMessage) { Log.d(TAG, "Keepalive received, sending auto restart broadcast for foregrounds service") sendBroadcast(Intent(this, SubscriberService.AutoRestartReceiver::class.java)) // Restart it if necessary! + val topic = remoteMessage.data["topic"] + if (topic != ApiService.CONTROL_TOPIC) { + Log.d(TAG, "Keepalive on non-control topic $topic received, subscribing to control topic ${ApiService.CONTROL_TOPIC}") + messenger.subscribe(ApiService.CONTROL_TOPIC) + } } private fun handleMessage(remoteMessage: RemoteMessage) {