updated notification

This commit is contained in:
KUNALROUT07 2025-04-16 15:58:24 +05:30
parent bb6c8b5ea4
commit b1f1f1e6ea
1 changed files with 44 additions and 2 deletions

View File

@ -12,6 +12,8 @@ import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.service.notification.StatusBarNotification; // for notification re-push
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import com.facebook.react.HeadlessJsTaskService; import com.facebook.react.HeadlessJsTaskService;
@ -43,10 +45,41 @@ public class ForegroundHeadlessService extends Service {
HeadlessJsTaskService.acquireWakeLockNow(context); HeadlessJsTaskService.acquireWakeLockNow(context);
// Schedule next execution // 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 @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
@ -57,14 +90,16 @@ public class ForegroundHeadlessService extends Service {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
this.handler.removeCallbacks(this.runnableCode); // Stop runnable execution this.handler.removeCallbacks(this.runnableCode); // Stop runnable execution
handler.removeCallbacks(notificationMonitorRunnable); // Stop monitoring
} }
private void createNotificationChannel() { private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel( NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, CHANNEL_ID,
"POC Location Gather Service", "POC Location Gather Service",
NotificationManager.IMPORTANCE_MIN NotificationManager.IMPORTANCE_DEFAULT
); );
NotificationManager manager = getSystemService(NotificationManager.class); NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel); manager.createNotificationChannel(channel);
@ -81,10 +116,14 @@ public class ForegroundHeadlessService extends Service {
// Intent headlessIntent = new Intent(getApplicationContext(), GeolocationHeadlessTask.class); // Intent headlessIntent = new Intent(getApplicationContext(), GeolocationHeadlessTask.class);
// getApplicationContext().startService(headlessIntent); // getApplicationContext().startService(headlessIntent);
this.handler.post(this.runnableCode); this.handler.post(this.runnableCode);
handler.post(notificationMonitorRunnable); // Start notification monitor
} else if (intent != null && intent.getAction() != null && intent.getAction().equals("STOP_FOREGROUND")) { } else if (intent != null && intent.getAction() != null && intent.getAction().equals("STOP_FOREGROUND")) {
Log.d(TAG, "Stopping foreground headless service"); Log.d(TAG, "Stopping foreground headless service");
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
handler.removeCallbacks(notificationMonitorRunnable); // Stop monitor
} }
return START_STICKY; return START_STICKY;
} }
@ -98,6 +137,9 @@ public class ForegroundHeadlessService extends Service {
.setContentText("Background location is active") .setContentText("Background location is active")
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setOngoing(true) // This makes it non-dismissible
.setPriority(NotificationCompat.PRIORITY_LOW) // Optional: Ensures visibility
.setAutoCancel(false) // prevent dismissal on tap
.build(); .build();
} }