Skip to content

Commit

Permalink
Open message notification to the correct conversation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jamesarich committed Nov 4, 2024
1 parent 7b18205 commit 4b7140f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
12 changes: 9 additions & 3 deletions app/src/main/java/com/geeksville/mesh/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4b7140f

Please sign in to comment.