coronalabs / corona

Solar2D Game Engine main repository (ex Corona SDK)
https://solar2d.com/
MIT License
2.54k stars 273 forks source link

Added check to exact scheduling for notifications #659

Closed zdmitrynsk closed 8 months ago

zdmitrynsk commented 11 months ago

SCHEDULE_EXACT_ALARM, the permission introduced in Android 12 for apps to schedule exact alarms, is no longer being pre-granted to most newly installed apps targeting Android 13 and higher (will be set to denied by default).

And because the SCHEDULE_EXACT_ALARM permission is now denied by default and the permission grant process requires extra steps from users (the user must minimize the application and change the setting), developers are strongly encouraged to evaluate their use cases and determine if exact alarms still make sense for their use cases.

Now for Android devices with SDK greater than or equal to 33, a check has been added for the ability to schedule notifications via AlarmManager.setExactAndAllowWhileIdle(). If this is not possible, then the notification is sent via AlarmManager.set().

For AlarmManager.set() only needs android.permission.POST_NOTIFICATIONS. It can be easily obtained through a modal window using native.showPopup( "requestAppPermission", options )

More information: https://developer.android.com/about/versions/14/changes/schedule-exact-alarms

CLAassistant commented 11 months ago

CLA assistant check
All committers have signed the CLA.

AleCGames commented 8 months ago

Great, thank you very much for this contribution. I still can't see anything in the documentation on how to implement these changes. I have several professional apps where the user can select whether they want to get notifications every 30 min, 1 hour, and so on. All of these apps are experiencing crashes due to this permission.

Shchvova commented 8 months ago

Working on it. But idea is to request SCHEDULE_EXACT_ALARM permission

AleCGames commented 8 months ago

Hi @Shchvova I've reviewed the documentation and they still haven't added how to check for exact alarms permission before scheduling notifications.

zdmitrynsk commented 7 months ago

I don't really understand what you need, but if you need to check whether the required permission is granted, then I think you should use the following code solar2d:

system.getInfo("androidGrantedAppPermissions").

The changes I made here allow applications installed on Android 13 or higher to send notifications with at least the android.permission.POST_NOTIFICATIONS permission. Previously, in order to send notifications, you must have the SCHEDULE_EXACT_ALARM permission. That is, previously it was impossible to send a notification with only android.permission.POST_NOTIFICATIONS (on Android 13 or higher)

With the following code you can request android.permission.POST_NOTIFICATIONS:

local options = { appPermission = "android.permission.POST_NOTIFICATIONS", urgency = "Critical", listener = function(event) end, } native.showPopup( "requestAppPermission", options )

AleCGames commented 7 months ago

Thank you for your explanation @zdmitrynsk I got it now!