4

I want to trigger a notification at 8:OO AM daily. Alarm manager is not working from android 9. I tried with work manager also but it's not working.

Currently am using alaram service, its working fine upto andorid 8 version, but not in 9.

Intent intent = new Intent(this, appstartreceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    long startUpTime = calendar.getTimeInMillis();

    // To avoid firing the alarm if the time is passed while setting
    if (System.currentTimeMillis() > startUpTime) {
        startUpTime = startUpTime + 24 * 60 * 60 * 1000;
    }

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);

Note : I want to trigger notification even app is killed/closed state.

RK191
  • 51
  • 4
  • 1
    Hello RK191, welcome to Stack Overflow! You're going to have to give us more details about what is it that _doesn't work_ exactly, what you've tried, and what you've found out so far; when in doubt, please check [the help section](https://stackoverflow.com/help/how-to-ask) on how to ask good questions, which will give you a better chance of getting a good answer; users will not usually write code out of the blue for free around here :) Good luck! (work manager should work, provided the constrains are set, but it *will not guarantee* exact time execution). – Martin Marconcini Aug 09 '19 at 10:43
  • (additionally, it would help if you describe the nature of the "task". You can certainly use a Timer and a TimerTask too, but overall, without much context, it's hard to know). – Martin Marconcini Aug 09 '19 at 10:45
  • @MartinMarconcini : Timer and TimerTask won't work if the app is closed. – RK191 Aug 09 '19 at 10:53
  • Correct, you'd need either a Foreground Service or an Alarm to wake you up via Intent Service or similar. When you say "not working" do you mean the alarm *does not trigger* ? Remember that all three of the JobScheduler, AlarmManager, and recent WorkManager are explicit about "not guaranteeing exact execution time" even if the constrains are met. Is it *really* important for the alarm to be at NN time? – Martin Marconcini Aug 09 '19 at 10:57
  • 1
    Perhaps instead of `setRepeating` you could use `setExact` or its idle variant `setExactAndAllowWhileIdle()` and within the alarm, re-schedule the next? If I am not mistaken (it's been a while), these were meant to be used in special cases, it's easier if you look at Alarm Manager's source code directly for options and behavior. – Martin Marconcini Aug 09 '19 at 11:03
  • source: https://developer.android.com/reference/android/app/AlarmManager.html#setExactAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent) – Martin Marconcini Aug 09 '19 at 11:05
  • I tried with `setExact` and `setExactAndAllowWhileIdle()`, its not triggering exactly 8:00 AM. Ex: Today its triggered at 8:00 AM and tomorrow its triggering 8:05 AM and day by day its changing. And some times its not triggering. Yes @MartinMarconcini its _really_ important. – RK191 Aug 09 '19 at 12:32
  • Ok Keep in mind that (*especially on higher APIs), these are *not precise* AT ALL. However, If one sets a "calendar" event, I'd expect it to fire at the exact time, so I'm surprised. Do you have a small reproducible project that exhibits this behavior? Does it consistently happen on Android 9? – Martin Marconcini Aug 09 '19 at 13:11

0 Answers0