fix: Fix images disappearing after replying

This commit is contained in:
PapaTutuWawa 2023-07-28 21:06:50 +02:00
parent adb8ee88d1
commit e975e749e4
3 changed files with 50 additions and 8 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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