Skip to content

Commit

Permalink
fix(android): fix an issue when using inexact alarms (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
wansiklee authored Jun 10, 2024
1 parent 0239339 commit f7c59f6
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions android/src/main/java/app/notifee/core/NotifeeAlarmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -146,9 +148,18 @@ static void scheduleTimestampTriggerNotification(

AlarmManager alarmManager = AlarmUtils.getAlarmManager();

TimestampTriggerModel.AlarmType alarmType = timestampTrigger.getAlarmType();

// Verify we can call setExact APIs to avoid a crash, but it requires an Android S+ symbol
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!alarmManager.canScheduleExactAlarms()) {

// Check whether the alarmType is the exact alarm
boolean isExactAlarm = Arrays
.asList(TimestampTriggerModel.AlarmType.SET_EXACT,
TimestampTriggerModel.AlarmType.SET_EXACT_AND_ALLOW_WHILE_IDLE,
TimestampTriggerModel.AlarmType.SET_ALARM_CLOCK)
.contains(alarmType);
if (isExactAlarm && !alarmManager.canScheduleExactAlarms()) {
System.err.println(
"Missing SCHEDULE_EXACT_ALARM permission. Trigger not scheduled. See:"
+ " https://notifee.app/react-native/docs/triggers#android-12-limitations");
Expand All @@ -159,8 +170,6 @@ static void scheduleTimestampTriggerNotification(
// Ensure timestamp is always in the future when scheduling the alarm
timestampTrigger.setNextTimestamp();

TimestampTriggerModel.AlarmType alarmType = timestampTrigger.getAlarmType();

switch (alarmType) {
case SET:
alarmManager.set(AlarmManager.RTC, timestampTrigger.getTimestamp(), pendingIntent);
Expand Down

0 comments on commit f7c59f6

Please sign in to comment.