From 58796ac31d8a5230bc8704d7208c8681a56f7c83 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sat, 29 Jun 2024 18:51:41 -0400 Subject: [PATCH] - Adds created_at as the time of the notification instead of the receiving time - Displays Zap Push notifications even if the zapped note is not available yet. --- .../EventNotificationConsumer.kt | 64 ++++++++++++++----- .../notifications/NotificationUtils.kt | 10 +++ 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt index 2be347507..5231262ce 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt @@ -161,6 +161,7 @@ class EventNotificationConsumer( event.id, content, user, + event.createdAt, userPicture, noteUri, applicationContext, @@ -201,7 +202,7 @@ class EventNotificationConsumer( val userPicture = note.author?.profilePicture() val noteUri = note.toNEvent() notificationManager() - .sendDMNotification(event.id, content, user, userPicture, noteUri, applicationContext) + .sendDMNotification(event.id, content, user, event.createdAt, userPicture, noteUri, applicationContext) } } } @@ -244,31 +245,63 @@ class EventNotificationConsumer( val author = LocalCache.getOrCreateUser(it.pubKey) val senderInfo = Pair(author, it.content.ifBlank { null }) - acc.decryptContent(noteZapped) { - Log.d("EventNotificationConsumer", "Notify Decrypted if Private Note") + if (noteZapped.event?.content() != null) { + acc.decryptContent(noteZapped) { + Log.d("EventNotificationConsumer", "Notify Decrypted if Private Note") - val zappedContent = it.split("\n").get(0) + val zappedContent = it.split("\n").get(0) + + val user = senderInfo.first.toBestDisplayName() + var title = stringRes(applicationContext, R.string.app_notification_zaps_channel_message, amount) + senderInfo.second?.ifBlank { null }?.let { title += " ($it)" } + + var content = + stringRes( + applicationContext, + R.string.app_notification_zaps_channel_message_from, + user, + ) + zappedContent.let { + content += + " " + + stringRes( + applicationContext, + R.string.app_notification_zaps_channel_message_for, + zappedContent, + ) + } + val userPicture = senderInfo?.first?.profilePicture() + val noteUri = "nostr:Notifications" + + Log.d("EventNotificationConsumer", "Notify ${event.id} $content $title $noteUri") + + notificationManager() + .sendZapNotification( + event.id, + content, + title, + event.createdAt, + userPicture, + noteUri, + applicationContext, + ) + } + } else { + // doesn't have a base note to refer to. + Log.d("EventNotificationConsumer", "Notify Zapped note not available") val user = senderInfo.first.toBestDisplayName() var title = stringRes(applicationContext, R.string.app_notification_zaps_channel_message, amount) senderInfo.second?.ifBlank { null }?.let { title += " ($it)" } - var content = + val content = stringRes( applicationContext, R.string.app_notification_zaps_channel_message_from, user, ) - zappedContent.let { - content += - " " + - stringRes( - applicationContext, - R.string.app_notification_zaps_channel_message_for, - zappedContent, - ) - } - val userPicture = senderInfo?.first?.profilePicture() + + val userPicture = senderInfo.first.profilePicture() val noteUri = "nostr:Notifications" Log.d("EventNotificationConsumer", "Notify ${event.id} $content $title $noteUri") @@ -278,6 +311,7 @@ class EventNotificationConsumer( event.id, content, title, + event.createdAt, userPicture, noteUri, applicationContext, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationUtils.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationUtils.kt index 4bc761c5d..f29ceb3e8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationUtils.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationUtils.kt @@ -90,6 +90,7 @@ object NotificationUtils { id: String, messageBody: String, messageTitle: String, + time: Long, pictureUrl: String?, uri: String, applicationContext: Context, @@ -101,6 +102,7 @@ object NotificationUtils { id, messageBody, messageTitle, + time, pictureUrl, uri, channelId, @@ -113,6 +115,7 @@ object NotificationUtils { id: String, messageBody: String, messageTitle: String, + time: Long, pictureUrl: String?, uri: String, applicationContext: Context, @@ -124,6 +127,7 @@ object NotificationUtils { id, messageBody, messageTitle, + time, pictureUrl, uri, channelId, @@ -136,6 +140,7 @@ object NotificationUtils { id: String, messageBody: String, messageTitle: String, + time: Long, pictureUrl: String?, uri: String, channelId: String, @@ -151,6 +156,7 @@ object NotificationUtils { id = id, messageBody = messageBody, messageTitle = messageTitle, + time = time, picture = imageResult.drawable as? BitmapDrawable, uri = uri, channelId, @@ -162,6 +168,7 @@ object NotificationUtils { id = id, messageBody = messageBody, messageTitle = messageTitle, + time = time, picture = null, uri = uri, channelId, @@ -175,6 +182,7 @@ object NotificationUtils { id: String, messageBody: String, messageTitle: String, + time: Long, picture: BitmapDrawable?, uri: String, channelId: String, @@ -218,6 +226,7 @@ object NotificationUtils { .setContentIntent(contentPendingIntent) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true) + .setWhen(time * 1000) // Build the notification val builder = @@ -236,6 +245,7 @@ object NotificationUtils { .setPublicVersion(builderPublic.build()) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true) + .setWhen(time * 1000) notify(notId, builder.build()) }