Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager doWork callback is not received in Redmi and other custom Chinese ROM when device got rebooted and force closed the app

My app is not receiving push notification in Redmi phones while the app is in the background or it got killed by swiping.

So I am trying to wake the phone by WorkManager which works on many phones except Redmi and other Chinese custom ROM phones.

Here is my code of Worker class

public class OpenTalkWorkManager extends Worker {

@NonNull
@Override
public Result doWork() {

    Log.i("wake_up", "Waking up now: " + System.currentTimeMillis());

    FirebaseUtils.getInstance().updateUserPresenceStatus(getApplicationContext(), "yes");

    Intent intent = new Intent("com.opentalk.WAKE_UP");
    getApplicationContext().sendBroadcast(intent);

    return Result.SUCCESS;
}

I am trying to enqueue the work through PeriodicWorkRequest

PeriodicWorkRequest.Builder mPeriodicWorkRequest = new PeriodicWorkRequest.Builder(OpenTalkWorkManager.class, 4, TimeUnit.MINUTES);
    Constraints myConstraints = new Constraints.Builder()
            .setRequiresBatteryNotLow(false)
            .setRequiredNetworkType(NetworkType.NOT_REQUIRED)
            .setRequiresCharging(false)
            .setRequiresDeviceIdle(false)
            .setRequiresStorageNotLow(false)

            // Many other constraints are available, see the
            // Constraints.Builder reference
            .build();
    PeriodicWorkRequest myWork = mPeriodicWorkRequest.setConstraints(myConstraints).build();

    UUID compressionWorkId = myWork.getId();
    WorkManager.getInstance().cancelWorkById(compressionWorkId);

    WorkManager.getInstance().enqueue(myWork);
like image 336
Sujeet Kumar Mehta Avatar asked Sep 15 '25 00:09

Sujeet Kumar Mehta


1 Answers

WorkManager API will work well as documented in the Android Stock OS devices or emulator. Customised ROMs like Xiaomi, Vivo, etc. have the Battery Saver option set to "optimized" by default, and don't allow the WorkManager API as expected. Set Battery Saver to "no restrictions" and your WorkManager will run the task.

I am using version 1.0.1 and it works well with Xiaomi devices after performing the above changes for Battery Saver.

like image 180
Kamal S Avatar answered Sep 16 '25 13:09

Kamal S