Clarification on Exact Notifications Using Unity Mobile Notifications on Android

I have been analyzing the behavior of the Unity Mobile Notifications plugin on Android and noticed something concerning the scheduling of repeating notifications. Based on my understanding of the native implementation:

When scheduling repeating notifications (even with the USE_EXACT_ALARM or SCHEDULE_EXACT_ALARM enabled), the plugin appears to rely on the AlarmManager.setInexactRepeating() method. As documented in the Android Developer documentation, this method is less battery-intensive but introduces significant inaccuracies, with delays ranging anywhere from 0 up to the full repeat interval:

“Your alarm’s first trigger will not be before the requested time, but it might not occur for almost a full interval after that time.”

This behavior is problematic for use cases where precise timing is critical. The more accurate alternative, setRepeating() does not seem to be used, even when USE_EXACT_ALARM or SCHEDULE_EXACT_ALARM mode is explicitly requested in Unity Notifications.

Could you confirm if this behavior is intentional? If so, is there a recommended way to achieve truly precise repeating notifications on Android using the Unity Mobile Notifications package?

Thank you for your assistance!

2 Likes

There are multiple levels to this: the setting, permission and settings on device. On some devices app gets installed with the thing off by default, user has to go to settings and enable it.
Unity does use exact scheduling when requested and available, however latest recommendation from Google are to avoid it. In real life scenarios notifications should not require very exact timing, couple minutes one way or the other should be acceptable, in our testing that’s about the accuracy one gets, unless power saving is enabled on device, but this case even exact notifications can get completely disabled.

https://docs.unity3d.com/Packages/com.unity.mobile.notifications@2.4/manual/Android.html#schedule-notifications-at-exact-time

Thank you for the clarification!

If I understand correctly, precise notifications can only be sent as one-time notifications. Are precise repeating notifications not supported by the Unity Mobile Notifications package due to Android’s recommendations to avoid them?

Oh, I see what you mean. This looks like a bug.

If it helps, I guess the issue might be in the Android plugin, specifically in the UnityNotificationManager.java file, within the scheduleNotificationIntentAlarm method. It seems that a check for canScheduleExactAlarms might need to be added for notifications with a repeat interval.

Brought the current implementation of notification scheduling from unity.mobile.notifications v2.3.2

// Call AlarmManager to set the broadcast intent with fire time and interval.
private void scheduleNotificationIntentAlarm(long repeatInterval, long fireTime, PendingIntent broadcast) {
    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

    if (repeatInterval <= 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && canScheduleExactAlarms(alarmManager)) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireTime, broadcast);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, fireTime, broadcast);
        }
    } else {
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, fireTime, repeatInterval, broadcast);
    }
}

Would you recommend that I create a bug report for this on Unity Issue Tracker?

Yes, please report.

1 Like