From 5fb3ae05364b5f44dbefc2650af48b3f4d08dd64 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Mon, 17 Jan 2022 14:06:59 -0500 Subject: [PATCH] Android-side fix for binary unifiedpush messages #101 --- app/src/main/java/io/heckel/ntfy/log/Log.kt | 3 +-- app/src/main/java/io/heckel/ntfy/msg/Message.kt | 3 +++ .../io/heckel/ntfy/msg/NotificationParser.kt | 8 +++++++- .../io/heckel/ntfy/firebase/FirebaseService.kt | 16 +++++++++++----- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/log/Log.kt b/app/src/main/java/io/heckel/ntfy/log/Log.kt index 59491a5..b79c92f 100644 --- a/app/src/main/java/io/heckel/ntfy/log/Log.kt +++ b/app/src/main/java/io/heckel/ntfy/log/Log.kt @@ -57,13 +57,12 @@ class Log(private val logsDao: LogsDao) { return getInstance()?.record?.get() ?: false } - fun init(context: Context): Log { + fun init(context: Context) { return synchronized(Log::class) { if (instance == null) { val database = Database.getInstance(context.applicationContext) instance = Log(database.logsDao()) } - instance!! } } diff --git a/app/src/main/java/io/heckel/ntfy/msg/Message.kt b/app/src/main/java/io/heckel/ntfy/msg/Message.kt index 8bd6060..51ed150 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/Message.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/Message.kt @@ -15,6 +15,7 @@ data class Message( val click: String?, val title: String?, val message: String, + val encoding: String?, val attachment: MessageAttachment?, ) @@ -26,3 +27,5 @@ data class MessageAttachment( val expires: Long?, val url: String, ) + +const val MESSAGE_ENCODING_BASE64 = "base64" diff --git a/app/src/main/java/io/heckel/ntfy/msg/NotificationParser.kt b/app/src/main/java/io/heckel/ntfy/msg/NotificationParser.kt index b63c421..a44e45a 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/NotificationParser.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/NotificationParser.kt @@ -1,5 +1,6 @@ package io.heckel.ntfy.msg +import android.util.Base64 import com.google.gson.Gson import io.heckel.ntfy.data.Attachment import io.heckel.ntfy.data.Notification @@ -19,6 +20,11 @@ class NotificationParser { if (message.event != ApiService.EVENT_MESSAGE) { return null } + val decodedMessage = if (message.encoding == MESSAGE_ENCODING_BASE64) { + String(Base64.decode(message.message, Base64.DEFAULT)) + } else { + message.message + } val attachment = if (message.attachment?.url != null) { Attachment( name = message.attachment.name, @@ -33,7 +39,7 @@ class NotificationParser { subscriptionId = subscriptionId, timestamp = message.time, title = message.title ?: "", - message = message.message, + message = decodedMessage, priority = toPriority(message.priority), tags = joinTags(message.tags), click = message.click ?: "", 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 cf7980d..096b731 100644 --- a/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt +++ b/app/src/play/java/io/heckel/ntfy/firebase/FirebaseService.kt @@ -1,6 +1,7 @@ package io.heckel.ntfy.firebase import android.content.Intent +import android.util.Base64 import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import io.heckel.ntfy.R @@ -23,11 +24,10 @@ class FirebaseService : FirebaseMessagingService() { private val job = SupervisorJob() private val messenger = FirebaseMessenger() - init { - Log.init(this) // Init in all entrypoints - } - override fun onMessageReceived(remoteMessage: RemoteMessage) { + // Init log (this is done in all entrypoints) + Log.init(this) + // We only process data messages if (remoteMessage.data.isEmpty()) { Log.d(TAG, "Discarding unexpected message (1): from=${remoteMessage.from}") @@ -63,6 +63,7 @@ class FirebaseService : FirebaseMessagingService() { val priority = data["priority"]?.toIntOrNull() val tags = data["tags"] val click = data["click"] + val encoding = data["encoding"] val attachmentName = data["attachment_name"] ?: "attachment.bin" val attachmentType = data["attachment_type"] val attachmentSize = data["attachment_size"]?.toLongOrNull() @@ -86,6 +87,11 @@ class FirebaseService : FirebaseMessagingService() { } // Add notification + val decodedMessage = if (encoding == MESSAGE_ENCODING_BASE64) { + String(Base64.decode(message, Base64.DEFAULT)) + } else { + message + } val attachment = if (attachmentUrl != null) { Attachment( name = attachmentName, @@ -100,7 +106,7 @@ class FirebaseService : FirebaseMessagingService() { subscriptionId = subscription.id, timestamp = timestamp, title = title ?: "", - message = message, + message = decodedMessage, priority = toPriority(priority), tags = tags ?: "", click = click ?: "",