Alarm Manager does not work when the application is killed on Android 7 (One Plus 3T)

Ever since Doze Mode and App StandBy have changed to manage alarms. The problem I am facing is that my alarm manager works correctly on KitKat, Lolipop, and Marshmellow devices, but above API 23 it does not work if the application is not in the foreground or in the background. But if the application is killed, the alarms will be stopped.

Checked Google Keep application on my Android 7, it turned out that it does the same.

But Google Calendar works regardless of whether the application is killed or not.

Some reading has been done and the setExactAndAllowWhileIdle method in the alarm manager has been found to interrupt the dosing mode and trigger an alarm.

But this does not work, is there something that I am missing here?

Here is my code:

 Intent alertIntent = new Intent(this, NotificationPublisher.class); alertIntent.putExtra("Hello", "Meal Time"); AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); int id = (int) System.currentTimeMillis(); if (Build.VERSION.SDK_INT < 23) { if (Build.VERSION.SDK_INT >= 19) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); } } else { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); } 

Broadcast Receiver:

 public class NotificationPublisher extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String message = intent.getStringExtra("Hello"); Log.d("Called", "Meal Time"); } 

manifest:

  <uses-permission android:name="android.permission.WAKE_LOCK" /> <receiver android:name=".NotificationPublisher" /> 
+8
android alarmmanager
source share
1 answer

Try adding the line below for your alertIntent myIntent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getBroadcast (this, 0, myIntent, 0);

-one
source share

All Articles