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 2638fda54..2ccd1a069 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 @@ -210,7 +210,10 @@ class EventNotificationConsumer( val content = chatNote.event?.content ?: "" val user = chatNote.author?.toBestDisplayName() ?: "" val userPicture = chatNote.author?.profilePicture() - val accountNpub = account.signer.pubKey.hexToByteArray().toNpub() + val accountNpub = + account.signer.pubKey + .hexToByteArray() + .toNpub() val chatroomMembers = chatRoom.users.joinToString(",") val noteUri = chatNote.toNEvent() + ACCOUNT_QUERY_PARAM + accountNpub @@ -224,6 +227,7 @@ class EventNotificationConsumer( noteUri, applicationContext, accountNpub = accountNpub, + accountPictureUrl = account.userProfile().profilePicture(), chatroomMembers = chatroomMembers, ) } @@ -254,20 +258,24 @@ class EventNotificationConsumer( val content = chatNote.event?.content ?: "" val user = chatNote.author?.toBestDisplayName() ?: "" val userPicture = chatNote.author?.profilePicture() - val accountNpub = account.signer.pubKey.hexToByteArray().toNpub() + val accountNpub = + account.signer.pubKey + .hexToByteArray() + .toNpub() val chatroomMembers = chatRoom.users.joinToString(",") val noteUri = chatNote.toNEvent() + ACCOUNT_QUERY_PARAM + accountNpub notificationManager() .sendDMNotification( - event.id, - content, - user, - event.createdAt, - userPicture, - noteUri, - applicationContext, + id = event.id, + messageBody = content, + senderName = user, + time = event.createdAt, + pictureUrl = userPicture, + uri = noteUri, + applicationContext = applicationContext, accountNpub = accountNpub, + accountPictureUrl = account.userProfile().profilePicture(), chatroomMembers = chatroomMembers, ) } @@ -297,19 +305,23 @@ class EventNotificationConsumer( decryptContent(note, account.signer)?.let { content -> val user = note.author?.toBestDisplayName() ?: "" val userPicture = note.author?.profilePicture() - val accountNpub = account.signer.pubKey.hexToByteArray().toNpub() + val accountNpub = + account.signer.pubKey + .hexToByteArray() + .toNpub() val noteUri = note.toNEvent() + ACCOUNT_QUERY_PARAM + accountNpub notificationManager() .sendDMNotification( - event.id, - content, - user, - event.createdAt, - userPicture, - noteUri, - applicationContext, + id = event.id, + messageBody = content, + senderName = user, + time = event.createdAt, + pictureUrl = userPicture, + uri = noteUri, + applicationContext = applicationContext, accountNpub = accountNpub, + accountPictureUrl = account.userProfile().profilePicture(), chatroomMembers = null, ) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationReplyReceiver.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationReplyReceiver.kt index b824a0f8c..f5009118e 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationReplyReceiver.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationReplyReceiver.kt @@ -34,6 +34,7 @@ import com.vitorpamplona.quartz.utils.Log import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import kotlin.coroutines.cancellation.CancellationException @@ -53,10 +54,13 @@ class NotificationReplyReceiver : BroadcastReceiver() { NotificationUtils.MARK_READ_ACTION -> { notificationManager.cancel(notificationId) } + NotificationUtils.REPLY_ACTION -> { - val replyText = RemoteInput.getResultsFromIntent(intent) - ?.getCharSequence(NotificationUtils.KEY_REPLY_TEXT) - ?.toString() + val replyText = + RemoteInput + .getResultsFromIntent(intent) + ?.getCharSequence(NotificationUtils.KEY_REPLY_TEXT) + ?.toString() if (replyText.isNullOrBlank()) return @@ -69,6 +73,13 @@ class NotificationReplyReceiver : BroadcastReceiver() { val pendingResult = goAsync() scope.launch { + // activates the relay to send the message. + val collectionJob = + scope.launch { + Amethyst.instance.relayProxyClientConnector.relayServices + .collect() + } + try { sendReply(accountNpub, members, replyText) notificationManager.cancel(notificationId) @@ -77,6 +88,9 @@ class NotificationReplyReceiver : BroadcastReceiver() { Log.e("NotificationReply", "Failed to send reply: ${e.message}") } finally { pendingResult.finish() + + // closes the relay connection. + collectionJob.cancel() } } } 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 58a408fa4..9c16a2e64 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 @@ -136,6 +136,7 @@ object NotificationUtils { uri: String, applicationContext: Context, accountNpub: String? = null, + accountPictureUrl: String? = null, chatroomMembers: String? = null, ) { getOrCreateDMChannel(applicationContext) @@ -151,6 +152,7 @@ object NotificationUtils { channelId = channelId, applicationContext = applicationContext, accountNpub = accountNpub, + accountPictureUrl = accountPictureUrl, chatroomMembers = chatroomMembers, ) } @@ -180,6 +182,7 @@ object NotificationUtils { channelId: String, applicationContext: Context, accountNpub: String?, + accountPictureUrl: String?, chatroomMembers: String?, ) { val notId = id.hashCode() @@ -187,8 +190,11 @@ object NotificationUtils { if (isDuplicate(notId)) return val bitmap = pictureUrl?.let { loadBitmap(it, applicationContext) } + val accountBitmap = accountPictureUrl?.let { loadBitmap(it, applicationContext) } val senderIcon = bitmap?.let { IconCompat.createWithBitmap(it) } + val accountIcon = accountBitmap?.let { IconCompat.createWithBitmap(it) } + val sender = Person .Builder() @@ -198,8 +204,13 @@ object NotificationUtils { val messagingStyle = NotificationCompat - .MessagingStyle(Person.Builder().setName("Me").build()) - .addMessage(messageBody, time * 1000, sender) + .MessagingStyle( + Person + .Builder() + .setName("Me") + .setIcon(accountIcon) + .build(), + ).addMessage(messageBody, time * 1000, sender) val contentIntent = Intent(applicationContext, MainActivity::class.java).apply { data = uri.toUri() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt index fa59d0dc4..d5269555f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt @@ -405,7 +405,9 @@ private fun NavigateIfIntentRequested( actionableNextPage?.let { nextRoute -> val npub = runCatching { URI(intentNextPage.removePrefix("nostr:")).findParameterValue("account") }.getOrNull() if (npub != null && accountSessionManager.currentAccountNPub() != npub) { - accountSessionManager.checkAndSwitchUserSync(npub, nextRoute) + accountSessionManager.checkAndSwitchUserSync(npub) { account -> + uriToRoute(intentNextPage, account) + } } else { val currentRoute = getRouteWithArguments(nextRoute::class, nav.controller) if (!isSameRoute(currentRoute, nextRoute)) { @@ -461,7 +463,9 @@ private fun NavigateIfIntentRequested( scope.launch { val npub = runCatching { URI(uri.removePrefix("nostr:")).findParameterValue("account") }.getOrNull() if (npub != null && accountSessionManager.currentAccountNPub() != npub) { - accountSessionManager.checkAndSwitchUserSync(npub, newPage) + accountSessionManager.checkAndSwitchUserSync(npub) { newAccount -> + uriToRoute(uri, newAccount) + } } else { val currentRoute = getRouteWithArguments(newPage::class, nav.controller) if (!isSameRoute(currentRoute, newPage)) { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountSessionManager.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountSessionManager.kt index 3549f612c..9e9fcf4b3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountSessionManager.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountSessionManager.kt @@ -112,11 +112,11 @@ class AccountSessionManager( } } - private suspend fun loginWithDefaultAccount(route: Route? = null) { + private suspend fun loginWithDefaultAccount(routeBuilder: ((account: Account) -> Route?)? = null) { val accountSettings = localPreferences.loadAccountConfigFromEncryptedStorage() if (accountSettings != null) { - startUI(accountSettings, route) + startUI(accountSettings, routeBuilder) } else { requestLoginUI() } @@ -184,11 +184,11 @@ class AccountSessionManager( fun startUI( accountSettings: AccountSettings, - route: Route? = null, + routeBuilder: ((account: Account) -> Route?)? = null, ) { val account = accountsCache.loadAccount(accountSettings) _accountContent.update { - AccountState.LoggedIn(account, route) + AccountState.LoggedIn(account, routeBuilder?.invoke(account)) } } @@ -280,7 +280,7 @@ class AccountSessionManager( localPreferences.setDefaultAccount(accountSettings) - startUI(accountSettings, route = Route.ImportFollowsSelectUser) + startUI(accountSettings, routeBuilder = { Route.ImportFollowsSelectUser }) scope.launch(Dispatchers.IO) { delay(2000) // waits for the new user to connect to the new relays. @@ -329,12 +329,12 @@ class AccountSessionManager( suspend fun checkAndSwitchUserSync( npub: String, - route: Route, + routeBuilder: ((account: Account) -> Route?)? = null, ): Boolean { if (npub != localPreferences.currentAccount()) { val account = localPreferences.allSavedAccounts().firstOrNull { it.npub == npub } if (account != null) { - switchUserSync(account, route) + switchUserSync(account, routeBuilder) return true } } @@ -343,10 +343,10 @@ class AccountSessionManager( private suspend fun switchUserSync( accountInfo: AccountInfo, - route: Route? = null, + routeBuilder: ((account: Account) -> Route?)? = null, ) { localPreferences.switchToAccount(accountInfo) - loginWithDefaultAccount(route) + loginWithDefaultAccount(routeBuilder) } fun currentAccountNPub() =