From 4b7140fbcd62190358b652c88ac433d521972612 Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:45:43 -0600 Subject: [PATCH] Open message notification to the correct conversation Adds an extra to the message notification intent to open the correct conversation. This ensures that when a user taps on a message notification, they are taken to the conversation with the sender of that message. --- .../main/java/com/geeksville/mesh/MainActivity.kt | 12 +++++++++--- .../mesh/service/MeshServiceNotifications.kt | 14 ++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index b068c85e2..dcdea088b 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -271,8 +271,11 @@ class MainActivity : AppCompatActivity(), Logging { } MeshServiceNotifications.OPEN_MESSAGE_ACTION -> { - val contactKey = intent.getStringExtra(MeshServiceNotifications.OPEN_MESSAGE_EXTRA_CONTACT_KEY) - showMessages(contactKey) + val contactKey = + intent.getStringExtra(MeshServiceNotifications.OPEN_MESSAGE_EXTRA_CONTACT_KEY) + val contactName = + intent.getStringExtra(MeshServiceNotifications.OPEN_MESSAGE_EXTRA_CONTACT_NAME) + showMessages(contactKey, contactName) } UsbManager.ACTION_USB_DEVICE_ATTACHED -> { @@ -604,8 +607,11 @@ class MainActivity : AppCompatActivity(), Logging { binding.pager.currentItem = 5 } - private fun showMessages(contactKey: String?) { + private fun showMessages(contactKey: String?, contactName: String?) { model.setCurrentTab(0) + if (contactKey != null && contactName != null) { + supportFragmentManager.navigateToMessages(contactKey, contactName) + } } override fun onCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt b/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt index 250324fa2..4a6392e7f 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt @@ -32,6 +32,8 @@ class MeshServiceNotifications( private const val FIFTEEN_MINUTES_IN_MILLIS = 15L * 60 * 1000 const val OPEN_MESSAGE_ACTION = "com.geeksville.mesh.OPEN_MESSAGE_ACTION" const val OPEN_MESSAGE_EXTRA_CONTACT_KEY = "com.geeksville.mesh.OPEN_MESSAGE_EXTRA_CONTACT_KEY" + const val OPEN_MESSAGE_EXTRA_CONTACT_NAME = + "com.geeksville.mesh.OPEN_MESSAGE_EXTRA_CONTACT_NAME" } private val notificationManager: NotificationManager get() = context.notificationManager @@ -169,7 +171,7 @@ class MeshServiceNotifications( fun updateMessageNotification(contactKey: String, name: String, message: String) = notificationManager.notify( - name.hashCode(), // show unique notifications, + contactKey.hashCode(), // show unique notifications, createMessageNotification(contactKey, name, message) ) @@ -189,12 +191,12 @@ class MeshServiceNotifications( ) } - private fun openMessageIntent(contactKey: String? = null): PendingIntent { + private fun openMessageIntent(contactKey: String, contactName: String): PendingIntent { val intent = Intent(context, MainActivity::class.java) intent.action = OPEN_MESSAGE_ACTION - contactKey?.let { - intent.putExtra(OPEN_MESSAGE_EXTRA_CONTACT_KEY, it) - } + intent.putExtra(OPEN_MESSAGE_EXTRA_CONTACT_KEY, contactKey) + intent.putExtra(OPEN_MESSAGE_EXTRA_CONTACT_NAME, contactName) + val pendingIntent = PendingIntent.getActivity( context, 0, @@ -269,7 +271,7 @@ class MeshServiceNotifications( } val person = Person.Builder().setName(name).build() with(messageNotificationBuilder) { - setContentIntent(openMessageIntent(contactKey)) + setContentIntent(openMessageIntent(contactKey, name)) priority = NotificationCompat.PRIORITY_DEFAULT setCategory(Notification.CATEGORY_MESSAGE) setAutoCancel(true)