Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send an event when a notification is dismissed #1012

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -142,6 +142,7 @@ class MessagingService : FirebaseMessagingService() {
val notificationManagerCompat = NotificationManagerCompat.from(this)

val tag = data["tag"]
val message = data[MESSAGE]
val messageId = tag?.hashCode() ?: System.currentTimeMillis().toInt()

var group = data["group"]
Expand Down Expand Up @@ -188,7 +189,7 @@ class MessagingService : FirebaseMessagingService() {

handleActions(notificationBuilder, tag, messageId, data)

handleDeleteIntent(notificationBuilder, messageId, group, groupId)
handleDeleteIntent(notificationBuilder, message, messageId, group, groupId)

handleContentIntent(notificationBuilder, messageId, group, groupId, data)

Expand Down Expand Up @@ -234,12 +235,14 @@ class MessagingService : FirebaseMessagingService() {

private fun handleDeleteIntent(
builder: NotificationCompat.Builder,
message: String?,
messageId: Int,
group: String?,
groupId: Int
) {

val deleteIntent = Intent(this, NotificationDeleteReceiver::class.java).apply {
putExtra(NotificationDeleteReceiver.EXTRA_MESSAGE, message)
putExtra(NotificationDeleteReceiver.EXTRA_NOTIFICATION_GROUP, group)
putExtra(NotificationDeleteReceiver.EXTRA_NOTIFICATION_GROUP_ID, groupId)
}
Expand Down
Expand Up @@ -3,17 +3,35 @@ package io.homeassistant.companion.android.notifications
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.util.cancelGroupIfNeeded
import javax.inject.Inject
import kotlinx.coroutines.runBlocking

class NotificationDeleteReceiver : BroadcastReceiver() {
companion object {
const val EXTRA_MESSAGE = "EXTRA_MESSAGE"
const val EXTRA_NOTIFICATION_GROUP = "EXTRA_NOTIFICATION_GROUP"
const val EXTRA_NOTIFICATION_GROUP_ID = "EXTRA_NOTIFICATION_GROUP_ID"
const val TAG = "NotifDeleteReceiver"
}

@Inject
lateinit var integrationRepository: IntegrationRepository

override fun onReceive(context: Context, intent: Intent) {

DaggerServiceComponent.builder()
.appComponent((context.applicationContext as GraphComponentAccessor).appComponent)
.build()
.inject(this)

val message = intent.getStringExtra(EXTRA_MESSAGE)
val group = intent.getStringExtra(EXTRA_NOTIFICATION_GROUP)
val groupId = intent.getIntExtra(EXTRA_NOTIFICATION_GROUP_ID, -1)

Expand All @@ -23,5 +41,23 @@ class NotificationDeleteReceiver : BroadcastReceiver() {
// This maybe the case if timeoutAfter has deleted the notification
// Then only the empty group is left and needs to be cancelled
notificationManagerCompat.cancelGroupIfNeeded(group, groupId)

val data = mutableMapOf(
"message" to message
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably be consistent with what we send into the events. We normally send the whole data object we receive with the notification not just the message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do some conversion in order to pass data properly, but now we get everything :)


runBlocking {
try {
integrationRepository.fireEvent("mobile_app_notification_dismissed", data)
Log.d(TAG, "Notification dismiss event successful!")
} catch (e: Exception) {
Log.e(TAG, "Issue sending event to Home Assistant", e)
Toast.makeText(
context,
R.string.notification_dismiss_failure,
Toast.LENGTH_LONG
).show()
}
}
}
}
Expand Up @@ -9,4 +9,6 @@ interface ServiceComponent {
fun inject(service: MessagingService)

fun inject(receiver: NotificationActionReceiver)

fun inject(receiver: NotificationDeleteReceiver)
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -136,6 +136,7 @@ Home Assistant instance</string>
<string name="manual_setup">enter address manually</string>
<string name="map">Map</string>
<string name="need_help">Need Help?</string>
<string name="notification_dismiss_failure">Failed to send event on notification dismissed</string>
<string name="nfc_btn_create_duplicate">Create duplicate</string>
<string name="nfc_btn_fire_event">Fire event</string>
<string name="nfc_btn_read_tag">Read NFC Tag</string>
Expand Down