Search code examples
androidalarmmanagerjob-schedulingandroid-workmanager

Android - Open app at specific time daily


I have an app with socket based push to app to open and show the user critical work like receiving orders on store. I have a requirement to sound an alarm 10 mins before store goes online daily. I decided go ahead with AlarmManager as below

Calendar alarmFor = Calendar.getInstance();
alarmFor.set(Calendar.HOUR_OF_DAY, 7);
alarmFor.set(Calendar.MINUTE, 45);
alarmFor.set(Calendar.SECOND, 0);

Intent MyIntent = new Intent(getApplicationContext(), BroadcastNotification.class);
PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, 
MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
MyAlarm.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmFor.getTimeInMillis(), 
MyPendIntent);

and then in onRecieve() will open the app as below with sound

@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyAPP", "onReceive() called");
// -> open app activity with alarm sound 
}

But above solution seems pretty old school.

I have also tried with WorkManager OneTimeRequest to set and trigger the job at specific time. But in Android Daily Job-Evernote it is mentioned that WorkManager donot guarantee to run at specific time and also daily job.

Request you to suggest better and unified approach to open the app with guarantee at specific time.

Thanks in advance.


Solution

  • AlarmManager won't work either. Doze mode prevents it (or rather, it will go off, but it may be as much as 15 minutes late). You have two options:

    1)Don't use websockets. Use FCM. FCM allows you to send a high priority push message which will wake the device.

    2)Have the user whitelist your from Doze mode. This cannot be done programaticly (the most you can do is launch the preference page to do it). This has to be done by the user by hand.

    I really suggest #1 if you want an end to end system. #2 and requiring the user to do something outside the app is always a bit iffy, and many users won't do it.