question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scheduling a notification causes a SecurityException on Android 12

See original GitHub issue

I’m getting a SecurityException: Caller needs to hold android.permission.SCHEDULE_EXACT_ALARM to set exact alarms on the following line: https://github.com/zo0r/react-native-push-notification/blob/e1a07166d5e291a5c117fdbd4b5a45472616e771/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java#L178

Looks to be new behaviour with API level 31 as per the docs https://developer.android.com/training/scheduling/alarms#exact

The system invokes an exact alarm at a precise moment in the future. If your app targets Android 12 (API level 31) or higher, you must declare the “Alarms & reminders” special app access; otherwise, a SecurityException occurs.

May need to mention somewhere in the docs to add the <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> in the AndroidManifest.xml.

As this permission can be disabled, the canScheduleExactAlarms() (docs) check looks to be needed before trying to schedule an exact alarm.

I’m happy to make a PR with the proposed changes if needed

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

7reactions
brianlenzcommented, Sep 12, 2022

I used this patch to check canScheduleExactAlarms():

diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
index 248ff0825af89ce82e2c7346ef1edc104b06c91c..a228a63f1033fac9622148de79928fed90e26d95 100644
--- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -170,6 +170,12 @@ public class RNPushNotificationHelper {
         if (pendingIntent == null) {
             return;
         }
+        
+        // call canScheduleExactAlarms to confirm the user has allowed alarms to be set. otherwise,
+        // attempting to set an alarm below will crash the app!
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !getAlarmManager().canScheduleExactAlarms()) {
+            return;
+        }
 
         Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
                 bundle.getString("id"), Long.toString(fireDate)));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Android 12: Using SCHEDULE_EXACT_ALARM permission ...
In the app I use foreground service and alarm clock to schedule update weather data from the cloud and device sensor and send...
Read more >
Behavior changes: Apps targeting Android 12
Android 12 changes the appearance and behavior of fully custom notifications. Previously, custom notifications were able to use the entire notification area and ......
Read more >
Exact alarm improvements-Android12 | by Nav Singh
SecurityException occurs if an app tries to use APIs that set exact alarms but isn't granted special app access. Ask users to grant...
Read more >
Configuring Scheduled Notifications in Android
If the app is targeting android 12 then developers need to handle a few cases explicitly, let's start setExact alarms. setExact Alarm types....
Read more >
Notifications - Expo Documentation
Starting from Android 12 (API level 31), to schedule the notification that triggers at the exact time, you need to add <uses-permission ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found