Send an event when a notification is dismissed (#1012)

* Send an event when a notification is dismissed

* Send all notification data instead of just the message

* Switch to a better method
This commit is contained in:
Daniel Shokouhi 2020-10-06 19:36:50 -07:00 committed by GitHub
parent a59ec710c6
commit 3efbbc073b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 1 deletions

View file

@ -228,7 +228,7 @@ class MessagingService : FirebaseMessagingService() {
handleActions(notificationBuilder, tag, messageId, data)
handleDeleteIntent(notificationBuilder, messageId, group, groupId)
handleDeleteIntent(notificationBuilder, data, messageId, group, groupId)
handleContentIntent(notificationBuilder, messageId, group, groupId, data)
@ -274,12 +274,15 @@ class MessagingService : FirebaseMessagingService() {
private fun handleDeleteIntent(
builder: NotificationCompat.Builder,
data: Map<String, String>,
messageId: Int,
group: String?,
groupId: Int
) {
val deleteIntent = Intent(this, NotificationDeleteReceiver::class.java).apply {
putExtra(NotificationDeleteReceiver.EXTRA_DATA, HashMap(data))
putExtra(NotificationDeleteReceiver.EXTRA_NOTIFICATION_GROUP, group)
putExtra(NotificationDeleteReceiver.EXTRA_NOTIFICATION_GROUP_ID, groupId)
}

View file

@ -3,17 +3,36 @@ 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_DATA = "EXTRA_DATA"
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)
@SuppressWarnings("unchecked")
val hashData = intent.getSerializableExtra(EXTRA_DATA) as HashMap<String, *>
val group = intent.getStringExtra(EXTRA_NOTIFICATION_GROUP)
val groupId = intent.getIntExtra(EXTRA_NOTIFICATION_GROUP_ID, -1)
@ -23,5 +42,19 @@ 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)
runBlocking {
try {
integrationRepository.fireEvent("mobile_app_notification_dismissed", hashData)
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()
}
}
}
}

View file

@ -9,4 +9,6 @@ interface ServiceComponent {
fun inject(service: MessagingService)
fun inject(receiver: NotificationActionReceiver)
fun inject(receiver: NotificationDeleteReceiver)
}

View file

@ -138,6 +138,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>