Resolves #577 request_location_update notification support
This commit is contained in:
		| @@ -9,6 +9,8 @@ import android.os.Build; | |||||||
|  |  | ||||||
| import androidx.core.app.NotificationCompat; | import androidx.core.app.NotificationCompat; | ||||||
| import androidx.work.ExistingPeriodicWorkPolicy; | import androidx.work.ExistingPeriodicWorkPolicy; | ||||||
|  | import androidx.work.ExistingWorkPolicy; | ||||||
|  | import androidx.work.OneTimeWorkRequest; | ||||||
| import androidx.work.PeriodicWorkRequest; | import androidx.work.PeriodicWorkRequest; | ||||||
| import androidx.work.WorkManager; | import androidx.work.WorkManager; | ||||||
|  |  | ||||||
| @@ -28,7 +30,10 @@ class LocationUtils { | |||||||
|     static final String SERVICE_NOTIFICATION_CHANNEL_ID = "location_service"; |     static final String SERVICE_NOTIFICATION_CHANNEL_ID = "location_service"; | ||||||
|     static final int SERVICE_NOTIFICATION_ID = 954311; |     static final int SERVICE_NOTIFICATION_ID = 954311; | ||||||
|  |  | ||||||
|  |     static final String REQUEST_LOCATION_NOTIFICATION = "request_location_update"; | ||||||
|  |  | ||||||
|     static final String LOCATION_WORK_NAME = "HALocationWorker"; |     static final String LOCATION_WORK_NAME = "HALocationWorker"; | ||||||
|  |     static final String LOCATION_REQUEST_NAME = "HALocationRequest"; | ||||||
|  |  | ||||||
|     static final int LOCATION_UPDATES_DISABLED = 0; |     static final int LOCATION_UPDATES_DISABLED = 0; | ||||||
|     static final int LOCATION_UPDATES_SERVICE = 1; |     static final int LOCATION_UPDATES_SERVICE = 1; | ||||||
| @@ -83,6 +88,18 @@ class LocationUtils { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     static void startWorker(Context context, long interval) { | ||||||
|  |         PeriodicWorkRequest periodicWork = new PeriodicWorkRequest.Builder(LocationUpdatesWorker.class, interval, TimeUnit.MILLISECONDS) | ||||||
|  |                 .build(); | ||||||
|  |         WorkManager.getInstance(context).enqueueUniquePeriodicWork(LocationUtils.LOCATION_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, periodicWork); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     static void requestLocationOnce(Context context) { | ||||||
|  |         OneTimeWorkRequest oneTimeWork = new OneTimeWorkRequest.Builder(LocationUpdatesWorker.class) | ||||||
|  |                 .build(); | ||||||
|  |         WorkManager.getInstance(context).enqueueUniqueWork(LocationUtils.LOCATION_REQUEST_NAME, ExistingWorkPolicy.REPLACE, oneTimeWork); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     static Notification getNotification(Context context, Location location, String channelId) { |     static Notification getNotification(Context context, Location location, String channelId) { | ||||||
|         CharSequence title = "Location tracking"; |         CharSequence title = "Location tracking"; | ||||||
|         CharSequence text = location == null ? "Accuracy: unknown" : "Accuracy: " + location.getAccuracy() + " m"; |         CharSequence text = location == null ? "Accuracy: unknown" : "Accuracy: " + location.getAccuracy() + " m"; | ||||||
| @@ -106,10 +123,4 @@ class LocationUtils { | |||||||
|  |  | ||||||
|         return builder.build(); |         return builder.build(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     static void startWorker(Context context, long interval) { |  | ||||||
|         PeriodicWorkRequest periodicWork = new PeriodicWorkRequest.Builder(LocationUpdatesWorker.class, interval, TimeUnit.MILLISECONDS) |  | ||||||
|                 .build(); |  | ||||||
|         WorkManager.getInstance(context).enqueueUniquePeriodicWork(LocationUtils.LOCATION_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, periodicWork); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -47,6 +47,15 @@ public class MessagingService extends FirebaseMessagingService { | |||||||
|     private void sendNotification(Map<String, String> data) { |     private void sendNotification(Map<String, String> data) { | ||||||
|         String channelId, messageBody, messageTitle, imageUrl, nTag, channelDescription; |         String channelId, messageBody, messageTitle, imageUrl, nTag, channelDescription; | ||||||
|         boolean autoCancel; |         boolean autoCancel; | ||||||
|  |         if (!data.containsKey("body")) { | ||||||
|  |             messageBody = ""; | ||||||
|  |         } else { | ||||||
|  |             messageBody = data.get("body"); | ||||||
|  |         } | ||||||
|  |         if (messageBody != null && messageBody.equals(LocationUtils.REQUEST_LOCATION_NOTIFICATION)) { | ||||||
|  |             LocationUtils.requestLocationOnce(this); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         String customChannelId = data.get("channelId"); |         String customChannelId = data.get("channelId"); | ||||||
|         if (customChannelId == null) { |         if (customChannelId == null) { | ||||||
|             channelId = "ha_notify"; |             channelId = "ha_notify"; | ||||||
| @@ -55,11 +64,6 @@ public class MessagingService extends FirebaseMessagingService { | |||||||
|             channelId = customChannelId; |             channelId = customChannelId; | ||||||
|             channelDescription = channelId; |             channelDescription = channelId; | ||||||
|         } |         } | ||||||
|         if (!data.containsKey("body")) { |  | ||||||
|             messageBody = ""; |  | ||||||
|         } else { |  | ||||||
|             messageBody = data.get("body"); |  | ||||||
|         } |  | ||||||
|         if (!data.containsKey("title")) { |         if (!data.containsKey("title")) { | ||||||
|             messageTitle = "HA Client"; |             messageTitle = "HA Client"; | ||||||
|         } else { |         } else { | ||||||
| @@ -106,7 +110,7 @@ public class MessagingService extends FirebaseMessagingService { | |||||||
|                         .setAutoCancel(autoCancel) |                         .setAutoCancel(autoCancel) | ||||||
|                         .setSound(defaultSoundUri) |                         .setSound(defaultSoundUri) | ||||||
|                         .setContentIntent(pendingIntent); |                         .setContentIntent(pendingIntent); | ||||||
|         Bitmap image; |         Bitmap image = null; | ||||||
|         if (URLUtil.isValidUrl(imageUrl)) { |         if (URLUtil.isValidUrl(imageUrl)) { | ||||||
|             image = getBitmapFromURL(imageUrl); |             image = getBitmapFromURL(imageUrl); | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user