Flutter Pro Design

Small details that build taste in Flutter.

curated by Kamran BekirovKamran Bekirov

Reschedule notifications when timezone or clock changes

When you schedule a notification with flutter_local_notifications's zonedSchedule, the plugin turns your TZDateTime into a UTC moment and gives that to the OS. The OS keeps only that moment. It doesn't remember the local time you meant.

So if the user flies to another country, or the clocks shift for daylight saving (twice a year in most of Europe and the US), the notification still fires at the old UTC moment. The local time is now wrong.

The fix is to notice the change and reschedule notifications. You need to compare two things: the timezone name (like Europe/London) and the current UTC offset (like +01:00).

The name changes when the user travels to another country. The offset changes during daylight saving.

Save them after each scheduling pass. On app resume, use flutter_timezone to read them again and compare:

Future<void> handleClockShift() async {
  final String name = (await FlutterTimezone.getLocalTimezone()).identifier;
  tz.setLocalLocation(tz.getLocation(name));
  
  final int offset = tz.TZDateTime.now(tz.local).timeZoneOffset.inMilliseconds;
  
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  final String? lastName = prefs.getString('last-tz-name');
  final int? lastOffset = prefs.getInt('last-tz-offset');
  
  if (lastName != null && (lastName != name || lastOffset != offset)) {
    await flutterLocalNotificationsPlugin.cancelAll();
    // also clear any local record you keep of "what's already scheduled"
    await rescheduleAllNotifications();
  }
  
  // Save current values for next comparison
  await prefs.setString('last-tz-name', name);
  await prefs.setInt('last-tz-offset', offset);
}

Run it on app open and resume (AppLifecycleListener.onResume). Users background the app more than they close it.

Even then, the fix only runs when the user comes back to the app. If hours of wrong notifications matter for your app (medication reminders, alarms), also reschedule once a day in the background.