From 598e3376bb1883ad421c95db58a3c06794475c6e Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 4 Jan 2022 21:10:36 +0100 Subject: [PATCH] Discard truncated FCM messages if instant delivery is enabled; closes #84 --- .../play/java/io/heckel/ntfy/firebase/FirebaseService.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 2970bb0..6f59637 100644 --- a/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt +++ b/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt @@ -56,6 +56,7 @@ class FirebaseService : FirebaseMessagingService() { val message = data["message"] val priority = data["priority"]?.toIntOrNull() val tags = data["tags"] + val truncated = (data["truncated"] ?: "") == "1" if (id == null || topic == null || message == null || timestamp == null) { Log.d(TAG, "Discarding unexpected message: from=${remoteMessage.from}, fcmprio=${remoteMessage.priority}, fcmprio_orig=${remoteMessage.originalPriority}, data=${data}") return @@ -65,8 +66,14 @@ class FirebaseService : FirebaseMessagingService() { CoroutineScope(job).launch { val baseUrl = getString(R.string.app_base_url) // Everything from Firebase comes from main service URL! - // Add notification + // Check if notification was truncated and discard if it will (or likely already did) arrive via instant delivery val subscription = repository.getSubscription(baseUrl, topic) ?: return@launch + if (truncated && subscription.instant) { + Log.d(TAG, "Discarding truncated message that did/will arrive via instant delivery: from=${remoteMessage.from}, fcmprio=${remoteMessage.priority}, fcmprio_orig=${remoteMessage.originalPriority}, data=${data}") + return@launch + } + + // Add notification val notification = Notification( id = id, subscriptionId = subscription.id,