cordova-plugin-local-notifications icon indicating copy to clipboard operation
cordova-plugin-local-notifications copied to clipboard

"Your use of exact alarms is causing your app to crash for some Android users" on Google Play Console.

Open TeoMastro opened this issue 1 year ago • 7 comments

I have a google play console error when using the plugin: Screenshot from 2024-02-08 12-41-12

Your Environment

  • Plugin version: latest
  • Platform: android
  • OS version: android 14 (target sdk 33)
  • Device manufacturer / model: Samsung S21 FE
  • Cordova version (cordova -v): 11
  • Cordova platform version (cordova platform ls):
  • Plugin config
  • Ionic Version (if using Ionic): Ionic 7

Expected Behavior

I should not be getting this google play console error.

Actual Behavior

I get a google play console error.

Steps to Reproduce

Just use the plugin, which works okay locally and upload it's build to Google Play Store for internal testing.

Context

Okay, time to explain what I did and what I have found so far regarding this issue. After reading the official android documentation: https://developer.android.com/develop/background-work/services/alarms/schedule about Scheduling alarms, I understood the following things:

  1. If your app targets Android 12 or higher, you must obtain the "Alarms & reminders" special app access.
  2. If your app targets Android 13 (API level 33) or higher, you have the option to declare either the SCHEDULE_EXACT_ALARM or the USE_EXACT_ALARM permission.

I use the Ionic 7 framework with Capacitor version 5, which targets the SKD 33. Without the use of any extra permission, I did not manage to get any notfication and make this plugin work on my samsung S21 FE (Android 14), although it was working properly on Google Pixel 6 Pro (which also has Android 14). This could be a One UI problem so I will assume it is not very relevant with this issue. After reading the google documentation about alarm scheduling, I used trapeze (https://trapeze.dev/) to add the "android.permission.USE_EXACT_ALARM" to the manifest of the project, which fixed the samsung s21 fe problem I mentioned earlier. I think this permission must be added, as the documentation states (see 2. above).

Note that the console error was also existing even before I was trying to change the permission and upload new builds, which indicates that the issue is probably plugin related. I assume that the SCHEDULE_EXACT_ALARM permission is somewhere asked by the plugin when exact notifications are being scheduled. If this is the case, the workflow for asking SCHEDULE_EXACT_ALARM permission has changed, and it is the following:

This, is an example taken from: https://codewithninad.com/using-android-schedule-exact-alarm-permission-explained/, but it is exactly what the android documentation states that must happen when requesting the SCHEDULE_EXACT_ALARM permission.

Check : canScheduleExactAlarms()

val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
when {
    alarmManager.canScheduleExactAlarms() -> {
         // permission granted
     }
     else -> {
         // open alarm permission screen
         Intent().apply {
            action = ACTION_REQUEST_SCHEDULE_EXACT_ALARM
         }.also {
            startActivity(it)
         }
      }
}

Use this broadcast receiver:

class AlarmReceiver : BroadcastReceiver() {
    private val TAG = "AlarmReceiver"
    override fun onReceive(context: Context, intent: Intent) {
    val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        when (intent.action) {
            ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED -> {
                if(alarmManager.canScheduleExactAlarms())
                    // Permission granted
            }
            "from alarm" -> {
                // Alarm fired
            }
        }
    }
}

Overall, I am not sure a solution like that would solve the problem, I just wanna see if anyone else has the same issue with the plugin. I am looking forward to receiving feedback, although I will keep doing my research on the topic. Unfortunately, I do not have enough native android experience to try and solve this issue by myself.

TeoMastro avatar Feb 08 '24 11:02 TeoMastro

I'm just starting a new app and this issue has already surfaced. Exact is a requirement for this app because it starts a countdown and no response can cause emergency services to be called. @katzer are you planning an upgrade?

ppetree avatar Feb 09 '24 23:02 ppetree

If you want, you can try my fork, which makes the plugin work for Android 14+:

https://github.com/TheNotorius0/cordova-plugin-local-notification

Keep in mind that this is a fork of the moodlemobile fork (https://github.com/moodlemobile/cordova-plugin-local-notification), which already works great for Android 13 devices, but notifications don't work for Android 14 there (but at least they didn't crash).

I have tested it with both API 33 and API 34, and with Allow setting alarms and reminders both enabled and disabled, and it works.

I haven't tested it in production yet though, since I literally created this new fork today.

TheNotorius0 avatar Feb 12 '24 15:02 TheNotorius0

Thanks! I'll give it a try. These notification plugins are a complete s* show! LOL

@bhandaribhumin did a fork and really did a nice job with documenting the properties but what's defined clashes with what's defined here (sound, led etc).

Mind you, I'm using Volt.build (a cloud build service that's a full install of cordova). I'm gonna do an Android build in a few... test it there on 6 and 10 and then I'll try yours for 6, 10 and 13 plus.

ppetree avatar Feb 12 '24 22:02 ppetree

@TheNotorius0 Your's is not yet on NPM so it's a no-go with volt.build.

ppetree avatar Feb 18 '24 18:02 ppetree

@TheNotorius0 Your's is not yet on NPM so it's a no-go with volt.build.

It seems that moodlemobile has released a new update: https://github.com/moodlemobile/cordova-plugin-local-notification.

I'm still using my fork so I haven't tested it yet, but notifications should now work for Android 14, and there is even a new method to make users open notification settings to schedule exact alarm, if the app requires precise timing.

TheNotorius0 avatar Feb 29 '24 18:02 TheNotorius0

moodlemobile doesn't have the createChannel function so it doesn't support custom sounds... neither does yours. That seems to be a common sticking point.

ppetree avatar Feb 29 '24 19:02 ppetree

Well, luckily I'm also using the cordova-plugin-firebasex Plugin, which supports the creation of channels, so I have never encountered that limitation.

TheNotorius0 avatar Feb 29 '24 21:02 TheNotorius0

If you want, you can try my fork, which makes the plugin work for Android 14+:

https://github.com/TheNotorius0/cordova-plugin-local-notification

Keep in mind that this is a fork of the moodlemobile fork (https://github.com/moodlemobile/cordova-plugin-local-notification), which already works great for Android 13 devices, but notifications don't work for Android 14 there (but at least they didn't crash).

I have tested it with both API 33 and API 34, and with Allow setting alarms and reminders both enabled and disabled, and it works.

I haven't tested it in production yet though, since I literally created this new fork today.

I have used your fork within my app but after uploading the production version to Google Play Console, the Pre-launch report shows stability crash error

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

bashartak avatar Jul 10 '24 20:07 bashartak

I have used your fork within my app but after uploading the production version to Google Play Console, the Pre-launch report shows stability crash error

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

That repository has been archived, you should use Moodle's one now: https://github.com/moodlemobile/cordova-plugin-local-notification

TheNotorius0 avatar Jul 10 '24 21:07 TheNotorius0

This is fixed with the latest master.

GitToTheHub avatar Aug 18 '24 11:08 GitToTheHub