Patches Welcome #7: Your Scheduled Notification Disappears If the User's Calendar Isn't Gregorian

2026-11-28来源 dev.toEnglish

zonedSchedule notifications on iOS and macOS fire correctly on most devices. Set the device calendar to Islamic, Buddhist, or Japanese, and the notification is silently dropped. No error, no callback, nothing in the console. It just doesn't fire.

How I found it

I was wiring up shift-end reminders in a workforce app. QA passed on every device we had. A field tester in the Middle East reported that reminders never arrived. Their iPhone was set to the Hijri calendar. That was the only difference.

The mechanism

flutter_local_notifications builds an NSDateComponents from the target date to create a UNCalendarNotificationTrigger. On iOS, FlutterLocalNotificationsPlugin.m:878 uses [NSCalendar currentCalendar] for the decomposition. On macOS, the Swift equivalent uses Calendar.current.

When the device calendar is Islamic, currentCalendar returns a Hijri calendar. Decomposing a 2026 Gregorian date into Hijri components gives you year 1448, month values in a different range, day counts that don't map. UNCalendarNotificationTrigger reads those components as Gregorian (it always does), sees a date in the past by roughly 578 years, and discards the trigger.

The input date is an ISO 8601 string parsed via ISO8601DateFormatter, which always produces a Gregorian date. The decomposition has to match.

The fix

Two lines. One per platform.

iOS (Objective-C):

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 

macOS (Swift):

var calendar = Calendar(identifier: .gregorian) 

2 additions, 2 deletions. The diff is symmetrical because the bug is symmetrical.

Why it went unnoticed

Most developers and most CI runners use Gregorian calendars. The plugin's own test suite doesn't mock a non-Gregorian calendar context. And the failure is silent: UNCalendarNotificationTrigger doesn't throw or log when the date components resolve to the past. It just never fires. You'd only catch this with a physical device (or simulator) set to a non-Gregorian locale and a notification you're actually waiting for.

I verified the fix on an iOS Simulator set to ar_SA (Islamic calendar). I haven't tested Buddhist (th_TH) or Japanese imperial, but the code path is identical: currentCalendar returns whatever the user has, and the fix bypasses it entirely.

PR #2867 · Issue #2866 · MaikuB/flutter_local_notifications

原文:https://www.a8q9.cn/en/article/patches-welcome-7-your-scheduled-notification-disappears-if-the-user-s-calendar--818082.html

相关文章