Alarm Manager broadcast is not received in some devices when app is not in memory ( swiped off from app-tray ) . Device in which it is not working runs with API 25 and is ONE PLUS 3 device. however same thing works in Nexus 6 (with API 25).
Strange thing is , I can see in system logs that Alarm is being triggered. I can see log below when alarm is supposed to go off. Just that broadCast is not received. again, this happens only when app is not in memory. Broadcast is received properly when app is in memory.
V/AlarmManager: Triggering alarm #0: 0 when =1508843147121 package=net.IntAppoperation =*walarm*:com.intapp.receivers.NOTIFICATION_ALARM
Here is how I'm setting alarm
In Manifest :
<receiver android:name="com.intapp.receivers.ReminderReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="com.intapp.receivers.NOTIFICATION_ALARM" />
</intent-filter>
</receiver>
And then When I want to set alarm :
public static final String ACTION = "com.intapp.receivers.NOTIFICATION_ALARM";
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.setAction(ACTION);
int randomNum = new Random().nextInt(Integer.MAX_VALUE - 1 + 1);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, randomNum, intent, PendingIntent.FLAG_ONE_SHOT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); // I get time from calendar instance in my function
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
}
Here's my implementation of Receiver :
public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Receiver called ");
// my implementation
}
}
EDIT:
Seems like problem is a little bigger. I have also registered for CALL_STATE
by which I can listen to incoming/outgoing calls. That broadcast receiver is also not firing when app is not in memory. Plus, This happens on Samsung device too running on API 25. Is this (Manifest broadcast not working) known problem for API 25 on some devices?