react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

App Crashed on Android 12

Open touchalex opened this issue 2 years ago • 13 comments

My android app crashed on Android 12 (Works fine on Android 11)

Here is my development environment:

  • MacBook Pro (14-inch, 2021) M1 Pro 16GB RAM
  • Mac OS 12.3.1 (21E258)
  • XCode 13.3 (13E113)

React Native Environment:

  • Homebrew 3.4.5

  • Node 17.8.0

  • NPM 8.5.5

  • Watchman 2022.03.21.00

  • React Native CLI 2.0.1

  • React Native 0.68

  • Pod 1.11.3

  • React Native Push Notificiation 8.1.1

and here is my build.gradle setting ext { buildToolsVersion = "31.0.0" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 ndkVersion = "21.4.7075529" googlePlayServicesVersion = "+" // default: "+" firebaseMessagingVersion = "21.1.0" // default: "21.1.0" supportLibVersion = "23.1.1" // default: 23.1.1 }

The app crash and close immediately when I open the app on Android 12 without showing any error.

touchalex avatar Apr 06 '22 08:04 touchalex

did you find any solution to this ? @touchalex

sa0sh0iip avatar Sep 09 '22 18:09 sa0sh0iip

@sa0sh0iip @touchalex it was due to this issue https://github.com/zo0r/react-native-push-notification/issues/2286

try adding this permission on AndroidManifest

  <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
  <uses-permission android:name="android.permission.USE_EXACT_ALARM"/>

damithg-dev avatar Oct 26 '22 06:10 damithg-dev

@sa0sh0iip @touchalex it was due to this issue #2286

try adding this permission on AndroidManifest

  <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
  <uses-permission android:name="android.permission.USE_EXACT_ALARM"/>

@damithg-dev It did not fix the issue in my case...Any update regarding this issue?

risav-sarkar avatar Nov 14 '22 09:11 risav-sarkar

@risav-sarkar for me it was fixed. and had to handle the permission. could you be able to share the error log

damithg-dev avatar Nov 14 '22 10:11 damithg-dev

1

@damithg-dev I am still getting this error

risav-sarkar avatar Nov 17 '22 07:11 risav-sarkar

@risav-sarkar for me it was fixed. and had to handle the permission. could you be able to share the error log

Hi, i have the same issue. Could you show how you handled the permission? Did you need to also display a promt after adding "SCHEDULE_EXACT_ALARM" and "USE_EXACT_ALARM"? In that case, how would this be done?

adrihegg avatar Nov 18 '22 15:11 adrihegg

Adding the following line, grade clean, delete app from testing device and rebuild helped.

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

According to https://developer.android.com/training/scheduling/alarms we need to prompt the user. No idea how to do that yet. After a quick test it seems to work fine with calling PushNotification.requestPermissions()

mklb avatar Nov 22 '22 10:11 mklb

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

This will work in ANDROID 12,13(default permission comes on ALLOW). But in ANDROID 14 default permission comes on DENIED. After that, this error will come again. Does anyone know how to request permission on SCHEDULE_EXACT_ALARM?

ThushalIntervest avatar Aug 16 '23 12:08 ThushalIntervest

Do you need the exact alarms? As in, is your application actually an alarm app, or an app where the main functionality depends on sending notifications at exact time?

In my case, my app does not qualify for exact alarm usage nor do I care if a notification is delivered at an exact time, therefore adding such permission makes no sense.

What I did to work around this issue was remove both SCHEDULE_EXACT_ALARM and USE_EXACT_ALARM permissions from AndroidManifest and patch the RNPushNotificationHelper.java file:

--- a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -173,15 +173,9 @@ public class RNPushNotificationHelper {
 
         Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
                 bundle.getString("id"), Long.toString(fireDate)));
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-            if (allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-                getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
-            } else {
-                getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
-            }
-        } else {
-            getAlarmManager().set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
-        }
+
+        // manual patch to use inexact alarm timing for scheduled notifications (avoids needing SCHEDULE_EXACT_ALARM permission)
+        getAlarmManager().setWindow(AlarmManager.RTC_WAKEUP, fireDate, 10 * 60 * 1000, pendingIntent);
     }
 

By using getAlarmManager().setWindow(AlarmManager.RTC_WAKEUP, fireDate, 10 * 60 * 1000, pendingIntent); we avoid the exact alarms, therefore we don't need the permission anymore and Android 12+ is now happy. I set the window at 10minutes because that was the recommended minimum in the Android docs. This code patch also removes SDK check because minSdk is 21 for my app.

lachtos avatar Sep 29 '23 10:09 lachtos

Do you need the exact alarms? As in, is your application actually an alarm app, or an app where the main functionality depends on sending notifications at exact time?

In my case, my app does not qualify for exact alarm usage nor do I care if a notification is delivered at an exact time, therefore adding such permission makes no sense.

What I did to work around this issue was remove both SCHEDULE_EXACT_ALARM and USE_EXACT_ALARM permissions from AndroidManifest and patch the RNPushNotificationHelper.java file:

--- a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -173,15 +173,9 @@ public class RNPushNotificationHelper {
 
         Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
                 bundle.getString("id"), Long.toString(fireDate)));
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-            if (allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-                getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
-            } else {
-                getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
-            }
-        } else {
-            getAlarmManager().set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
-        }
+
+        // manual patch to use inexact alarm timing for scheduled notifications (avoids needing SCHEDULE_EXACT_ALARM permission)
+        getAlarmManager().setWindow(AlarmManager.RTC_WAKEUP, fireDate, 10 * 60 * 1000, pendingIntent);
     }
 

By using getAlarmManager().setWindow(AlarmManager.RTC_WAKEUP, fireDate, 10 * 60 * 1000, pendingIntent); we avoid the exact alarms, therefore we don't need the permission anymore and Android 12+ is now happy. I set the window at 10minutes because that was the recommended minimum in the Android docs. This code patch also removes SDK check because minSdk is 21 for my app.

Unfortunately, this suggestion still causes a bug in AOS 14 that causes the app to crash.

WayneKim92 avatar Jan 12 '24 06:01 WayneKim92

Unfortunately, this suggestion still causes a bug in AOS 14 that causes the app to crash.

By AOS 14 do you mean Android 14? I have tested on a physical Pixel 6a with Android 14 and on simulators and there doesn’t seem to be a problem. The patch is also live with thousands of users and I’m not getting crash reports related to this. Can you post the crash info?

lachtos avatar Jan 12 '24 07:01 lachtos

@damithg-dev correct one is

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

(taken from https://stackoverflow.com/a/74397306/7867180). Take care when using USE_EXACT_ALARM as stated in the docs

CyxouD avatar Feb 26 '24 08:02 CyxouD

@damithg-dev correct one is

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

(taken from https://stackoverflow.com/a/74397306/7867180). Take care when using USE_EXACT_ALARM as stated in the docs

It's worked!

k-lpmg avatar Apr 29 '24 07:04 k-lpmg