Android Notifications triggered by Alarm Manager not Firing when App is in Doze Mode

前端 未结 5 1030
無奈伤痛
無奈伤痛 2021-02-01 09:14

I have the following requirements. A user needs to be able to schedule a recurring reminder in my app that will trigger a push notification at an exact time ev

相关标签:
5条回答
  • 2021-02-01 09:47

    Yes, you are right. Now, Google recommends using WorkManager instead of AlarmManager. AlarmManager has many limits in his work. You can find more information here (doze mode)
    Also, pixel phone has a bug with alarmManager

    0 讨论(0)
  • 2021-02-01 09:47

    You would not be able to run background services long running in Oreo as there are behaviour changes, now Oreo to optimise system memory, battery etc, it kills background service, to solve your issue you should use foreground service.

    Have a look at Background execution limits https://developer.android.com/about/versions/oreo/android-8.0-changes

    A suggestion from me, if you can use FCM then go for it, becasue apps like WeChat, Facebook uses it, to deliver notifications and they don't face any problem...

    Hope this helps in understanding the issue....

    0 讨论(0)
  • 2021-02-01 09:52

    I have faced the similar problems. I tried with work manager but the result was same. When phone is locked and is in doze mode the event are not triggered.

    But for triggering event at exact time you need to use alarm manager only. It should work even in doze mode.

    Method to register alarm manger(use set exact time instead of repeating)

    public static void registerAlarm(Context context){
        final int FIVE_MINUTES_IN_MILLI = 300000;
        final int THIRTY_SECOND_IN_MILLI = 30000;
        long launchTime = System.currentTimeMillis() + FIVE_MINUTES_IN_MILLI;
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, BroadcastAlarmManger.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, launchTime, pi);
        else am.setExact(AlarmManager.RTC_WAKEUP, launchTime, pi);
        Utility.printLog("timestamp "+launchTime);
    }
    

    Broadcast receiver for alarm

    public class BroadcastAlarmManger extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //register alarm again
             registerAlarm(context);
            .
            .
            .
            .
            //do your stuff    
        }
    }
    

    Manifest declaration

    <receiver
            android:name="com.taxiemall.utility.BroadcastAlarmManger"
            android:enabled="true"
            android:exported="true"/>
    

    Also you have to handle Reboot manually. After every reboot registered alarm are cleared. So register a broadcast receiver for Android reboot and register you alarm again inside it.

    0 讨论(0)
  • 2021-02-01 09:54

    Adding an intent Flag FLAG_RECEIVER_FOREGROUND

    https://developer.android.com/reference/android/content/Intent#FLAG_RECEIVER_FOREGROUND prior to calling the broadcast receiver should do the trick

    Intent intent = new Intent(context, ScheduleAllReceiver.class);
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    PendingIntent scheduleAllPendingIntent = PendingIntent.getBroadcast(context, SCHEDULER_DAILY_ALL, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    0 讨论(0)
  • 2021-02-01 09:56

    This is the library that worked for me.

    0 讨论(0)
提交回复
热议问题