implement request_location_update via push notifications (#333)

This commit is contained in:
Jörg Thalheim 2020-02-04 14:31:26 +00:00 committed by GitHub
parent 33ffe4a70c
commit 48dd9192bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View file

@ -32,6 +32,8 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
companion object {
const val ACTION_REQUEST_LOCATION_UPDATES =
"io.homeassistant.companion.android.background.REQUEST_UPDATES"
const val ACTION_REQUEST_ACCURATE_LOCATION_UPDATE =
"io.homeassistant.companion.android.background.REQUEST_ACCURATE_UPDATE"
const val ACTION_PROCESS_LOCATION =
"io.homeassistant.companion.android.background.PROCESS_UPDATES"
const val ACTION_PROCESS_GEO =
@ -55,6 +57,7 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
ACTION_REQUEST_LOCATION_UPDATES -> setupLocationTracking(context)
ACTION_PROCESS_LOCATION -> handleLocationUpdate(context, intent)
ACTION_PROCESS_GEO -> handleGeoUpdate(context, intent)
ACTION_REQUEST_ACCURATE_LOCATION_UPDATE -> requestSingleAccurateLocation(context)
else -> Log.w(TAG, "Unknown intent action: ${intent.action}!")
}
}

View file

@ -13,6 +13,7 @@ import androidx.core.content.ContextCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.background.LocationBroadcastReceiver
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.domain.integration.IntegrationUseCase
import io.homeassistant.companion.android.webview.WebViewActivity
@ -62,11 +63,27 @@ class MessagingService : FirebaseMessagingService() {
}
remoteMessage.notification?.let {
if (it.body == "request_location_update") {
Log.d(TAG, "Request location update")
if (actions.size != 0) {
Log.w(TAG, "Ignoring received actions since location update was requested")
}
requestAccurateLocationUpdate()
return
}
Log.d(TAG, "Message Notification: ${it.title} -> ${it.body}")
sendNotification(it.title, it.body!!, actions)
}
}
private fun requestAccurateLocationUpdate() {
val intent = Intent(this, LocationBroadcastReceiver::class.java)
intent.action = LocationBroadcastReceiver.ACTION_REQUEST_ACCURATE_LOCATION_UPDATE
sendBroadcast(intent)
}
/**
* Create and show a simple notification containing the received FCM message.
*