diff --git a/android/app/src/main/java/com/poclocationgather/ForegroundHeadlessService.java b/android/app/src/main/java/com/poclocationgather/ForegroundHeadlessService.java index 654b694..886de92 100644 --- a/android/app/src/main/java/com/poclocationgather/ForegroundHeadlessService.java +++ b/android/app/src/main/java/com/poclocationgather/ForegroundHeadlessService.java @@ -12,6 +12,8 @@ import android.os.Handler; import android.os.IBinder; import android.util.Log; +import android.service.notification.StatusBarNotification; // for notification re-push + import androidx.core.app.NotificationCompat; import com.facebook.react.HeadlessJsTaskService; @@ -43,10 +45,41 @@ public class ForegroundHeadlessService extends Service { HeadlessJsTaskService.acquireWakeLockNow(context); // Schedule next execution - handler.postDelayed(this, 180000); + handler.postDelayed(this, 60000); } }; + + + // NEW: Runnable to monitor and re-push notification every 1 mins + private Runnable notificationMonitorRunnable = new Runnable() { + @Override + public void run() { + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + boolean isNotificationVisible = false; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications(); + for (StatusBarNotification sbn : activeNotifications) { + if (sbn.getId() == NOTIFICATION_ID) { + isNotificationVisible = true; + break; + } + } + } + + if (!isNotificationVisible) { + Log.d(TAG, "Notification not found - re-pushing"); + notificationManager.notify(NOTIFICATION_ID, createNotification()); + } else { + Log.d(TAG, "Notification is active"); + } + + handler.postDelayed(this, TimeUnit.MINUTES.toMillis(1)); + } + }; + + @Override public void onCreate() { super.onCreate(); @@ -57,14 +90,16 @@ public class ForegroundHeadlessService extends Service { public void onDestroy() { super.onDestroy(); this.handler.removeCallbacks(this.runnableCode); // Stop runnable execution + handler.removeCallbacks(notificationMonitorRunnable); // Stop monitoring } + private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "POC Location Gather Service", - NotificationManager.IMPORTANCE_MIN + NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); @@ -81,10 +116,14 @@ public class ForegroundHeadlessService extends Service { // Intent headlessIntent = new Intent(getApplicationContext(), GeolocationHeadlessTask.class); // getApplicationContext().startService(headlessIntent); this.handler.post(this.runnableCode); + handler.post(notificationMonitorRunnable); // Start notification monitor + } else if (intent != null && intent.getAction() != null && intent.getAction().equals("STOP_FOREGROUND")) { Log.d(TAG, "Stopping foreground headless service"); stopForeground(true); stopSelf(); + + handler.removeCallbacks(notificationMonitorRunnable); // Stop monitor } return START_STICKY; } @@ -98,6 +137,9 @@ public class ForegroundHeadlessService extends Service { .setContentText("Background location is active") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pendingIntent) + .setOngoing(true) // This makes it non-dismissible + .setPriority(NotificationCompat.PRIORITY_LOW) // Optional: Ensures visibility + .setAutoCancel(false) // prevent dismissal on tap .build(); }