From e975e749e45e4c1638462fa3a32562ae5dc5ad83 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Fri, 28 Jul 2023 21:06:50 +0200 Subject: [PATCH] fix: Fix images disappearing after replying --- .../polynom/moxplatform_android/Constants.kt | 14 +++++--- .../NotificationReceiver.kt | 33 +++++++++++++++++-- .../moxplatform_android/Notifications.kt | 11 +++++-- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt index 78b8798..c23ba9f 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt @@ -13,13 +13,19 @@ const val REPLY_TEXT_KEY = "key_reply_text" const val MARK_AS_READ_ID_KEY = "notification_id" // Values for actions performed through the notification -const val REPLY_ACTION = "reply"; +const val REPLY_ACTION = "reply" const val MARK_AS_READ_ACTION = "mark_as_read" -const val TAP_ACTION = "tap"; +const val TAP_ACTION = "tap" // Extra data keys for the intents that reach the NotificationReceiver -const val NOTIFICATION_EXTRA_JID_KEY = "jid"; -const val NOTIFICATION_EXTRA_ID_KEY = "notification_id"; +const val NOTIFICATION_EXTRA_JID_KEY = "jid" +const val NOTIFICATION_EXTRA_ID_KEY = "notification_id" + +// Extra data keys for messages embedded inside the notification style +const val NOTIFICATION_MESSAGE_EXTRA_MIME = "mime" +const val NOTIFICATION_MESSAGE_EXTRA_PATH = "path" + +const val MOXPLATFORM_FILEPROVIDER_ID = "me.polynom.moxplatform_android.fileprovider" // TODO: Maybe try again to rewrite the entire plugin in Kotlin //const val METHOD_CHANNEL_KEY = "me.polynom.moxplatform_android" diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt index 7e7c03b..6ec8bf2 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt @@ -12,7 +12,9 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.app.Person import androidx.core.app.RemoteInput +import androidx.core.content.FileProvider import me.polynom.moxplatform_android.Api.NotificationEvent +import java.io.File import java.time.Instant class NotificationReceiver : BroadcastReceiver() { @@ -83,11 +85,37 @@ class NotificationReceiver : BroadcastReceiver() { conversationTitle = recoveredStyle.conversationTitle // TODO: Use person recoveredStyle.messages.forEach { - addMessage(Notification.MessagingStyle.Message(it.text, it.timestamp, it.sender)) + // Check if we have to request (or refresh) the content URI to be able to still + // see the embedded image. + val mime = it.extras.getString(NOTIFICATION_MESSAGE_EXTRA_MIME) + val path = it.extras.getString(NOTIFICATION_MESSAGE_EXTRA_PATH) + val message = Notification.MessagingStyle.Message(it.text, it.timestamp, it.sender) + if (mime != null && path != null) { + // Request a new URI from the file provider to ensure we can still see the image + // in the notification + val fileUri = FileProvider.getUriForFile( + context, + MOXPLATFORM_FILEPROVIDER_ID, + File(path), + ) + message.setData( + mime, + fileUri, + ) + + // As we're creating a new message, also recreate the additional metadata + message.extras.apply { + putString(NOTIFICATION_MESSAGE_EXTRA_MIME, mime) + putString(NOTIFICATION_MESSAGE_EXTRA_PATH, path) + } + } + + // Append the old message + addMessage(message) } } - // TODO: Images get lost here? Do we have to request a new content URI? + // Append our new message newStyle.addMessage( Notification.MessagingStyle.Message( replyPayload!!, @@ -96,6 +124,7 @@ class NotificationReceiver : BroadcastReceiver() { ) ) + // Post the new notification val recoveredBuilder = Notification.Builder.recoverBuilder(context, notification).apply { style = newStyle setOnlyAlertOnce(true) diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt index ecaa52a..13a2ecb 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt @@ -110,10 +110,17 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif if (message.content.mime != null && message.content.path != null) { val fileUri = FileProvider.getUriForFile( context, - "me.polynom.moxplatform_android.fileprovider", + MOXPLATFORM_FILEPROVIDER_ID, File(message.content.path), ) - msg.setData(message.content.mime, fileUri) + msg.apply { + setData(message.content.mime, fileUri) + + extras.apply { + putString(NOTIFICATION_MESSAGE_EXTRA_MIME, message.content.mime) + putString(NOTIFICATION_MESSAGE_EXTRA_PATH, message.content.path) + } + } } // Append the message