1

I have Foreground Service in my application which sends API request every minute. Here is the code:

public class MyService extends Service {
  @Override
    public void onCreate() {
      Notification notification = getNotification();
      int notificationId = 20;
      startForeground(notificationId, notification);


      Observable.interval(0, 60, TimeUnit.SECONDS)
           .doOnNext(aLong -> sendMyRequest())
           .subscribe()

    }
}

And I have WorkManager with PeriodicWorkRequest which starts (or restarts) my service every 15 minutes:

@Override
public Result doWork() {
   context.stopService(new Intent(context, MyService.class));
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(new Intent(context, MyService .class));
   } else {
        context.startService(new Intent(context, MyService .class));
   }
    
    return Result.success();
}

And it works correct when I use the app or when I close the app and have some activity in my device. But when I don't use the device and it is in sleep mode, I see that restarting service works well (every 15 minutes), but app doesn't send requests. As I understand, it happens cause device is in doze mode (OS version is 10) and this mode prevents requests sending. But I really need this functionality (sending request every minute when Foreground Service works). How can I solve this issue?

Alex D.
  • 1,424
  • 15
  • 40
  • Doze mode was there long before android 10, oreo perhaps, so the best way to deal with critical info notification is to use fcm, once you receive the token your device will wake up from doze and you can request api or whatever. – hemen Jul 23 '20 at 06:26
  • did you find any solution to this? I have the same issue with wear os – Andrea Nisticò Sep 11 '20 at 14:02

0 Answers0