Sample _configureLocalTimeZone needs refactor for Linux
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
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,
);
});
}
}
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
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).