mirror of
https://github.com/home-assistant/android
synced 2024-09-19 16:11:37 +00:00
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:
parent
a59ec710c6
commit
3efbbc073b
|
@ -228,7 +228,7 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
|
|
||||||
handleActions(notificationBuilder, tag, messageId, data)
|
handleActions(notificationBuilder, tag, messageId, data)
|
||||||
|
|
||||||
handleDeleteIntent(notificationBuilder, messageId, group, groupId)
|
handleDeleteIntent(notificationBuilder, data, messageId, group, groupId)
|
||||||
|
|
||||||
handleContentIntent(notificationBuilder, messageId, group, groupId, data)
|
handleContentIntent(notificationBuilder, messageId, group, groupId, data)
|
||||||
|
|
||||||
|
@ -274,12 +274,15 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
|
|
||||||
private fun handleDeleteIntent(
|
private fun handleDeleteIntent(
|
||||||
builder: NotificationCompat.Builder,
|
builder: NotificationCompat.Builder,
|
||||||
|
data: Map<String, String>,
|
||||||
messageId: Int,
|
messageId: Int,
|
||||||
group: String?,
|
group: String?,
|
||||||
groupId: Int
|
groupId: Int
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
val deleteIntent = Intent(this, NotificationDeleteReceiver::class.java).apply {
|
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, group)
|
||||||
putExtra(NotificationDeleteReceiver.EXTRA_NOTIFICATION_GROUP_ID, groupId)
|
putExtra(NotificationDeleteReceiver.EXTRA_NOTIFICATION_GROUP_ID, groupId)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,17 +3,36 @@ package io.homeassistant.companion.android.notifications
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.core.app.NotificationManagerCompat
|
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 io.homeassistant.companion.android.util.cancelGroupIfNeeded
|
||||||
|
import javax.inject.Inject
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
class NotificationDeleteReceiver : BroadcastReceiver() {
|
class NotificationDeleteReceiver : BroadcastReceiver() {
|
||||||
companion object {
|
companion object {
|
||||||
|
const val EXTRA_DATA = "EXTRA_DATA"
|
||||||
const val EXTRA_NOTIFICATION_GROUP = "EXTRA_NOTIFICATION_GROUP"
|
const val EXTRA_NOTIFICATION_GROUP = "EXTRA_NOTIFICATION_GROUP"
|
||||||
const val EXTRA_NOTIFICATION_GROUP_ID = "EXTRA_NOTIFICATION_GROUP_ID"
|
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) {
|
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 group = intent.getStringExtra(EXTRA_NOTIFICATION_GROUP)
|
||||||
val groupId = intent.getIntExtra(EXTRA_NOTIFICATION_GROUP_ID, -1)
|
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
|
// This maybe the case if timeoutAfter has deleted the notification
|
||||||
// Then only the empty group is left and needs to be cancelled
|
// Then only the empty group is left and needs to be cancelled
|
||||||
notificationManagerCompat.cancelGroupIfNeeded(group, groupId)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,4 +9,6 @@ interface ServiceComponent {
|
||||||
fun inject(service: MessagingService)
|
fun inject(service: MessagingService)
|
||||||
|
|
||||||
fun inject(receiver: NotificationActionReceiver)
|
fun inject(receiver: NotificationActionReceiver)
|
||||||
|
|
||||||
|
fun inject(receiver: NotificationDeleteReceiver)
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,6 +138,7 @@ Home Assistant instance</string>
|
||||||
<string name="manual_setup">enter address manually</string>
|
<string name="manual_setup">enter address manually</string>
|
||||||
<string name="map">Map</string>
|
<string name="map">Map</string>
|
||||||
<string name="need_help">Need Help?</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_create_duplicate">Create duplicate</string>
|
||||||
<string name="nfc_btn_fire_event">Fire event</string>
|
<string name="nfc_btn_fire_event">Fire event</string>
|
||||||
<string name="nfc_btn_read_tag">Read NFC Tag</string>
|
<string name="nfc_btn_read_tag">Read NFC Tag</string>
|
||||||
|
|
Loading…
Reference in a new issue