I am trying to push local notification through my app by using flutter_local_notifications package and as on FlutterNotificationsPlugin the ".schedule" is deprecated I tried using ".zonedSchedule" but it requires TZDateTime value.
var androidDetails = AndroidNotificationDetails(t.id, "Task Notifier",
"Notifies if you have a task scheduled at a particular time");
var generalNotificationDetails =
NotificationDetails(android: androidDetails);
var now = DateTime.now();
var scheduledTime = DateTime(
now.year, now.month, _dayTasks.date.day, t.time.hour, t.time.minute)
.subtract(Duration(minutes: 5));
//use zonedSchedule once you figure out how to convert datetime to TZdatetime
// await notifications.zonedSchedule(
// 0, "Task", t.description, scheduledTime, generalNotificationDetails,
// androidAllowWhileIdle: true,
// uiLocalNotificationDateInterpretation:
// UILocalNotificationDateInterpretation.wallClockTime);
await notifications.schedule(0, "Ideal Day Task", t.description,
scheduledTime, generalNotificationDetails);
I had just tried to solve the same problem. To understand this, we will need to have a basic understanding of platform channel.
https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-kotlin-tab
Once we understand that, the code in the sample should become more understandable: https://github.com/MaikuB/flutter_local_notifications/blob/1fe78b9d129d674cd97cfba49734577b24c56925/flutter_local_notifications/example/android/app/src/main/java/com/dexterous/flutterlocalnotificationsexample/MainActivity.java
Concept in action:
First of all, we will need to import tz like so. This can be done in your main.dart or a helper file.
To create a
TZDateTime
from a plain dateTime, we will need to use the device timezone through queried through the platform channel.Where
getTimeZoneName
is implemented in the platform specific implementation.Be sure to call
configureLocalTimeZone
before the next step.And when we want to create the
TZDateTime
from aDateTime
object, we can usually just do like soThe full implementation is showcased in the example code here: https://github.com/MaikuB/flutter_local_notifications/blob/1fe78b9d129d674cd97cfba49734577b24c56925/flutter_local_notifications/example/lib/main.dart
I have not considered the different edge cases in my very simple work flow so YMMV. Hope this helps! :)