I want my react-native headless JS running in the background to perform a job every 30 seconds. When there is a criteria match I want a notification alert on my Android phone. Everything works perfectly before the phone gets dozed. The alarm job manages to trigger the notification message on-time like what I configured in my alarm manager.
Not until the phone dozes then it manages to shows notifications when only the phone gets screen awake. It does actually work but not by my expectation. The screen wake only happens approximately every 4-5 minutes once, and it's not every 30 seconds. I even attempted to add another alarm job (2 alarm schedule jobs), and it still won't make it happen. My purpose is to have my phone at least showing dim display when onReceive() is triggered. What have I missed? Or what should I do?
public class AlarmHelper {
private static final int PI_SELF_SCHEDULED_ALARM = 1000;
private static final int PI_REPEATING_ALARM = 1001;
private final Context context;
private AlarmManager alarmManager;
public AlarmHelper(Context context) {
this.context = context;
alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
}
public void setNextSelfSchedulingAlarm() {
final long fourtySecondsFromNow = System.currentTimeMillis() + SECONDS.toMillis(30);
Intent exactIntent = new Intent(context, AlarmBroadcastReceiver.class).setAction(SELF_SCHEDULING_ALARM);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, PI_SELF_SCHEDULED_ALARM, exactIntent, FLAG_UPDATE_CURRENT);
if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(RTC_WAKEUP, fourtySecondsFromNow, pendingIntent);
} else if (SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(RTC_WAKEUP, fourtySecondsFromNow, pendingIntent);
} else {
alarmManager.set(RTC_WAKEUP, fourtySecondsFromNow, pendingIntent);
}
}
public void startRepeatingAlarm() {
final int repeatingIntervalInSeconds = 30;
final long tenSecondsFromNow = System.currentTimeMillis() + SECONDS.toMillis(30);
Intent repeatingIntent = new Intent(context, AlarmBroadcastReceiver.class).setAction(REPEATING_ALARM);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, PI_REPEATING_ALARM, repeatingIntent, FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(RTC_WAKEUP, tenSecondsFromNow, SECONDS.toMillis(repeatingIntervalInSeconds), pendingIntent);
}
}
&
public class AlarmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock screenWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "ScreenLock tag from AlarmListener");
try {
screenWakeLock.acquire();
Hawk.init(context).build();
String action = intent.getAction();
if (action == null) {
screenWakeLock.release();
return;
}
if (REPEATING_ALARM.equals(action)) {
} else if (SELF_SCHEDULING_ALARM.equals(action)) {
AlarmHelper alarmHelper = new AlarmHelper(context);
alarmHelper.setNextSelfSchedulingAlarm();
}
Intent intentService = new Intent(context, HeadlessTaskService.class);
context.startService(intentService);
} finally {
screenWakeLock.release();
}
}
}