3

My requirement is set Notification for specific time like user birthday or holiday

I am using AlarmManager for scheduling notification using broadcast receiver

Code is working fine in 6.0 (even when app is killed,swiped from recent list) but not working on Android 8.1.0 (Mf : Oppo)

Readed This and This and many answers but not find any helpfull any idea how to solve this issue

Here is my code

    AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

    //creating a new intent specifying the broadcast receiver\

    Intent i = new Intent(this, HolidayBroadcast.class);
    i.putExtra("eventName",islamicHoliday.getEventName());
    i.putExtra("dateH", testTmp.getCalendar().getTimeInMillis());
    i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    //creating a pending intent using the intent
    PendingIntent pi = PendingIntent.getBroadcast(this, new Random().nextInt(), i, PendingIntent.FLAG_UPDATE_CURRENT);


    //setting alarm
    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,getTimeInMillis(), pi);
    }
    else
    {
        am.setExact(AlarmManager.RTC_WAKEUP, getTimeInMillis(), pi);
    }
Joker
  • 153
  • 14
  • refer this , https://stackoverflow.com/a/46305462/1848157 and https://medium.com/@iiro.krankka/its-time-to-kiss-goodbye-to-your-implicit-broadcastreceivers-eefafd9f4f8a – Radhey May 02 '19 at 13:32

2 Answers2

1

You can use workmanager as mentioned in link.It supports all api version and easy to use because it uses backward compatibility to API level 14

https://developer.android.com/topic/libraries/architecture/workmanager

https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712

tushar
  • 56
  • 3
  • 14
  • can you give some example of job sheduler i try but not working for shedule job after one year – Joker May 03 '19 at 06:58
  • but if you use work manager then there is no need to use of job scheduler. – tushar May 03 '19 at 11:46
  • ohk can you provide minimal example of workmanager which can helps to schedule jobs – Joker May 03 '19 at 12:06
  • You can find tutorials online and take a look at mentioned link: https://stackoverflow.com/questions/50363541/schedule-a-work-on-a-specific-time-with-workmanager – tushar May 03 '19 at 13:23
  • What if I need to fire a task at specific time? Workmanager is not suitable for this (tasks can be deferred) – Vadim Kotov May 13 '20 at 12:24
0

If your task is time critical, WorkManager API is not recommended as for your alarm manager not working properly, on Android 8 use AlarmManager.setexactandallowwhileidle(), to set exact time and allow wakeup notification while app is killed.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253