flutter_local_notifications icon indicating copy to clipboard operation
flutter_local_notifications copied to clipboard

Sample _configureLocalTimeZone needs refactor for Linux

Open bksubhuti opened this issue 7 months ago • 2 comments

For _zonedScheduleNotification in the sample.. the following needs to be called in main.

Future<void> _configureLocalTimeZone() async {
  tz.initializeTimeZones();  // call this first
  if (kIsWeb) {
    return;
  }
  if (Platform.isWindows) {
    return;
  }
  final String? timeZoneName = await FlutterTimezone.getLocalTimezone();
  tz.setLocalLocation(tz.getLocation(timeZoneName!));
}

Below is my own implementation that needs tx.initializeTimeZones taken from sample code.

Future _zonedScheduleNotification(Duration duration) async { final scheduledTime = tz.TZDateTime.now(tz.local).add(duration);

const notificationDetails = NotificationDetails(
  android: AndroidNotificationDetails(
    'my_time_schedule_channel',
    'My Time Schedule Timer',
    channelDescription:
        'Notifications for your My Time Schedule timer sessions.',
    sound: RawResourceAndroidNotificationSound('bell'),
    playSound: true,
    importance: Importance.max,
    priority: Priority.high,
  ),
  iOS: DarwinNotificationDetails(
    sound: 'bell.wav', // Ensure it's included in iOS assets
  ),
  macOS: DarwinNotificationDetails(sound: 'bell.wav'),
);

if (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) {
  await flutterLocalNotificationsPlugin.zonedSchedule(
    0,
    'My Time Schedule',
    'Your session has ended.',
    scheduledTime,
    notificationDetails,
    androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
  );
} else if (Platform.isLinux || Platform.isWindows) {
  // Fallback: simulate schedule with delayed Timer
  Timer(duration, () async {
    await flutterLocalNotificationsPlugin.show(
      0,
      'My Time Schedule',
      'Your session has ended.',
      notificationDetails,
    );
  });
}

}

bksubhuti avatar May 27 '25 03:05 bksubhuti

Can you elaborate more on what the problem is that you're trying to communicate? Currently I have to guess from the code snippets you have shared. The purpose of zonedSchedule() is to schedule a notification based on a timezone. The notification will fire even when the app is terminated and if a platform implements that method then expectation is it will behave like this on all platforms. Your scenario on using a timer is different as it will not work if the app is terminated. Perhaps this works with your app but I should mention this as perhaps you weren't aware of this and may have bug in your app

MaikuB avatar Jul 05 '25 06:07 MaikuB

I appreciate you getting back to me. If i recall, I think there was a small mistake in the sample code. And that small code snippet in the front of the post was needed to be called in the main in order for it to work. However, I have done so many revisions now, I 'm not sure what was done back then.

I have implemented notifications and I think it works fine by scheduling the whole day. However, I think that the daily scheduling of notifications does not work. Because of that I shut it down and have a "past 12" detector. The code seems to work well below: I am grateful for your work. https://github.com/mytimeschedule/my_time_schedule

One more thing.. i noticed that custom sounds button on your sample app does not work while in Linux and of course, my app could not work in linux with custom sounds (or any sound).

bksubhuti avatar Jul 09 '25 13:07 bksubhuti