bhandaribhumin / cordova-plugin-local-notification-12

Apache License 2.0
17 stars 35 forks source link

Android 14 SCHEDULE_EXACT_ALARM permission required. Permission is now defaulted to false even with manifest tag. #15

Open BradCB opened 6 months ago

BradCB commented 6 months ago

Android 14+ schedule notifications will not fire unless the permission is explicitly allowed. More info here: https://developer.android.com/training/scheduling/alarms#using-schedule-exact-permission.

It's not really an issue with this plugin. It's just a new requirement for Android 14+ to get the permission explicitly before any scheduling is permitted.

ppetree commented 3 months ago

Yeah, this needs some code to resolve this for us developers.

danemco commented 1 month ago

Any update on this?

ppetree commented 1 month ago

So, the best I have managed to come up with is to use the mixon plugin for notifications, use the @havesource push notification plugin to create the channel for custom sounds and then there's another plugin that handles just the permissions on Android 14. I still have to test it all but I know mixon works so far and the channels I'm using for custom push sounds so everything should work. Be next week before I can get back to this one.

jondspa commented 1 month ago

Here's my solution. I added some lines in dummyNotifications() to call the new Alarms and Reminders settings page. The user enables a toggle there and then notifications work on Android 14.

I'm not much of a Java programmer but this works.

In platforms/android/app/src/main/java/de/appplant/cordova/plugin/localnotification/LocalNotification.java in the function dummyNotifications.

Add this near the top: import android.app.AlarmManager;

After this section:

  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }

Add these lines:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            // check and set alarms
            AlarmManager mAlarmManager = context.getSystemService(AlarmManager.class);
            if (mAlarmManager.canScheduleExactAlarms() == false) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
                    intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                    cordova.startActivityForResult(this, intent, REQUEST_PERMISSIONS_CALL);
           }
   }