Skip to content

Commit d9a2d41

Browse files
committed
Implement exact scheduling control and support on android 12+
1 parent 644f1d9 commit d9a2d41

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static android.app.Notification.VISIBILITY_PUBLIC;
2929

3030
import java.lang.Integer;
31+
import java.lang.reflect.Method;
3132
import java.util.Calendar;
3233
import java.util.Random;
3334
import java.util.Set;
@@ -50,6 +51,8 @@ public class UnityNotificationManager extends BroadcastReceiver {
5051
private HashSet<Integer> mVisibleNotifications;
5152
private ConcurrentHashMap<Integer, Notification.Builder> mScheduledNotifications;
5253
private NotificationCallback mNotificationCallback;
54+
private int mExactSchedulingSetting = -1;
55+
private Method mCanScheduleExactAlarms;
5356

5457
static final String TAG_UNITY = "UnityNotifications";
5558

@@ -513,15 +516,28 @@ synchronized List<Notification.Builder> loadSavedNotifications() {
513516
return intent_data_list;
514517
}
515518

516-
private static boolean canScheduleExactAlarms(AlarmManager alarmManager) {
517-
// The commented-out if below is the correct one and should replace the one further down
518-
// However it requires compile SDK 31 to compile, cutting edge and not shipped with Unity at the moment of writing this
519-
// It means exact timing for notifications is not supported on Android 12+ out of the box
520-
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
521-
//return alarmManager.canScheduleExactAlarms();
522-
if (Build.VERSION.SDK_INT >= 31)
519+
private boolean canScheduleExactAlarms(AlarmManager alarmManager) {
520+
// exact scheduling supported since Android 6
521+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
523522
return false;
524-
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
523+
if (mExactSchedulingSetting < 0)
524+
mExactSchedulingSetting = mContext.getApplicationInfo().metaData.getInt("com.unity.androidnotifications.exact_scheduling", 1);
525+
if (mExactSchedulingSetting == 0)
526+
return false;
527+
if (Build.VERSION.SDK_INT < 31)
528+
return true;
529+
530+
try {
531+
if (mCanScheduleExactAlarms == null)
532+
mCanScheduleExactAlarms = AlarmManager.class.getMethod("canScheduleExactAlarms");
533+
return (boolean)mCanScheduleExactAlarms.invoke(alarmManager);
534+
} catch (NoSuchMethodException ex) {
535+
Log.e(TAG_UNITY, "No AlarmManager.canScheduleExactAlarms() on Android 31+ device, should not happen", ex);
536+
return false;
537+
} catch (Exception ex) {
538+
Log.e(TAG_UNITY, "AlarmManager.canScheduleExactAlarms() threw", ex);
539+
return false;
540+
}
525541
}
526542

527543
// Call AlarmManager to set the broadcast intent with fire time and interval.

0 commit comments

Comments
 (0)