From f7c59f6c4ef257c38918ffef0b2ec5a1f438a190 Mon Sep 17 00:00:00 2001 From: iamsik Date: Tue, 11 Jun 2024 01:50:01 +0900 Subject: [PATCH] fix(android): fix an issue when using inexact alarms (#981) --- .../app/notifee/core/NotifeeAlarmManager.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/app/notifee/core/NotifeeAlarmManager.java b/android/src/main/java/app/notifee/core/NotifeeAlarmManager.java index fc5b078e..5db42321 100644 --- a/android/src/main/java/app/notifee/core/NotifeeAlarmManager.java +++ b/android/src/main/java/app/notifee/core/NotifeeAlarmManager.java @@ -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; @@ -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"); @@ -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);