I'm using flutter_local_notifications to schedule notifications at specific times every day.
The user can select different time zones, it works on Android but doesn't work on iOS, as I don't have experience with iOS, I don't know how it handles notifications.
Future<void> _showNotification(
BuildContext? context,
NotificationDetails platformChannelSpecifics,
Prayer prayer,
DateTime dateTime) async {// dateTime is the time in another time zone, and time is the time passed to the zonedScheduled method
var time = _nextInstanceOfPrayer(dateTime);
if (time == null) {
return;
}
await flutterLocalNotificationsPlugin.zonedSchedule(
_getId(time, prayer: prayer.toString()),
prayer.toStr(context!),
_getPrayerText(prayer.toStr(context)),
time,
platformChannelSpecifics,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);}
And this method to get the time:
tzZone.TZDateTime? _nextInstanceOfPrayer(DateTime dateTime) {
final DateTime now = DateTime.now();
//converts time to specific location
tzZone.TZDateTime scheduledDate = tzZone.TZDateTime.from(
dateTime.subtract(now.timeZoneOffset),
tzZone.UTC,
);
if (scheduledDate.toUtc().isBefore(now.toUtc())) {
return null;
}
return scheduledDate;}
What I'm trying is add notification at the time (time) in phone's local time, for example, the user selects a +5 GMT time zone (5:00PM) and the local time zone is say +2 GMT (2:00PM), I want to add a notification with the local time of (5:00PM +5 GMT) and show it on (5:00 +2 GMT).
I have tried to convert both times to UTC and show the notification on UTC - local time zone (as shown in the second code), the code shows there are pending notifications but when I change the time of the device it doesn't show anything.
Note: when I add a manual time after 5 seconds, it works perfectly
DateTime.now().add(Duration(seconds: 5));