3

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);
Sujeet Kumar Mehta
  • 2,761
  • 2
  • 20
  • 28

4 Answers4

1

Please also take a look at this Xiaomi, Vivo, Oppo and huawei restrict background services by default. After applying this solution my WorkManager also started to work.

Behrad Ranjbar
  • 261
  • 2
  • 13
0

Which version of WorkManager are you using ? We fixed a few bugs with respect to PeriodicWork and force-stopping of apps in alpha05. alpha06 is out, and I recommend you try using it. If you still have the same problem, please report back on the issue tracker with a reproducible test case on the issue tracker.

Rahul
  • 19,744
  • 1
  • 25
  • 29
  • Rahul, Thanks for your answer. I am currently using alpha05. I am going to try it out with alpha06 than I will let you know the report. – Sujeet Kumar Mehta Aug 03 '18 at 04:18
  • I tried with alpha06 and tested that it actually starts after reboot in Redme if autostart is enabled in it if Autostart is not enabled than doWork callback is not received. Also when it start after reboot than in doWork internet call are not successfull in background. Please let me know if I can use alpha06 for my prod app. What is the tentative date of a stable release of this api? – Sujeet Kumar Mehta Aug 03 '18 at 07:21
  • I have created a new issue in issue tracker id is: 112159719 – Sujeet Kumar Mehta Aug 03 '18 at 09:53
  • 13
    @Rahul Why is WorkManager not working in xiomi devices if autostart is off(by default autostart is off), am using alpha08 also in all new device like, vivo,oppo,oneplus, etc they restrict background service, Then what is the point of using Jobscheduler or WorkManager. Most of the user in market are device i mentioned above, So can you tell me how to make this work, We need a clear solution from google,Because this is a serious issue – Samwinishere Here Aug 29 '18 at 07:24
  • 1
    @SamwinishereHere Did you find any solution? I am also struggling with the same issue, I have found some very hacky ways to do this but I am looking for some good practises. – Divyanshu Negi Jan 29 '19 at 22:05
  • @Rahul any updates on that issue! or still it is open? – Radhey May 07 '19 at 05:49
  • 1
    i am using `androidx.work:work-runtime:2.1.0` and still not executed(Xiomi), only when i open the app it start executing, on emulator work fine – Pavel Poley Jul 26 '19 at 14:30
  • @PavelPoley true. Even I am using the same version and am facing the same issue. It used to work fine with MIUI 8 & 9 but with MIUI 10, it has doesn't really work as expected. – Vrajesh Hirani Aug 14 '19 at 10:39
0

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />

Add these permissions in your manifest file it may work.

RANAJEET BARIK
  • 185
  • 2
  • 7
0

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.

Kamal S
  • 26
  • 6