Search code examples
flutterflutter-local-notification

Flutter schedule local notification does not work


I am trying to create a notification at a certain time but it is not working. The code does not throw any error but no notification is displayed on the device. I am using flutter_local_notifications

Code that generates the notification:

Future<void> showNotification(
  int hour, int id, String title, String body) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
  id,
  title,
  body,
  _convertTime(hour),
  const NotificationDetails(
    android: AndroidNotificationDetails('main_channel', 'Main Channel',
        channelDescription: "ashwin",
        importance: Importance.max,
        priority: Priority.max),
    iOS: IOSNotificationDetails(
      sound: 'default.wav',
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
    ),
  ),
  uiLocalNotificationDateInterpretation:
      UILocalNotificationDateInterpretation.absoluteTime,
  androidAllowWhileIdle: true,
);
}

_converTime function code:

TZDateTime _convertTime(int hour) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduleDate =
    tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, 47);
return scheduleDate;
}

The result of the function is correct and is as follows: 2022-07-14 12:47:00.000Z

However, if instead of using this function, I change it to

Future<void> showNotification(
  int hour, int id, String title, String body) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
  id,
  title,
  body,
  // _convertTime(hour), // this does not work
  tz.TZDateTime.now(tz.local).add(Duration(seconds: 1)), // this work
  const NotificationDetails(
    android: AndroidNotificationDetails('main_channel', 'Main Channel',
        channelDescription: "ashwin",
        importance: Importance.max,
        priority: Priority.max),
    // iOS details
    iOS: IOSNotificationDetails(
      sound: 'default.wav',
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
    ),
  ),

  uiLocalNotificationDateInterpretation:
      UILocalNotificationDateInterpretation.absoluteTime,
  androidAllowWhileIdle: true,
);
}

I don't understand what could be happening. I appreciate any help in advance.


Solution

  • try this one, remove some properties if you don't want them...

     Future zonedScheduleNotification(String note, DateTime date, occ) async {
            // IMPORTANT!!
            //tz.initializeTimeZones(); --> call this before using tz.local (ideally put it in your init state)
            
            int id = math.Random().nextInt(10000);
            log(date.toString());
            log(tz.TZDateTime.parse(tz.local, date.toString())
                .toString());
            try {
              await flutterLocalNotificationsPlugin.zonedSchedule(
                id,
                occ,
                note,
                tz.TZDateTime.parse(tz.local, date.toString()),
                NotificationDetails(
                  android: AndroidNotificationDetails(
                      'your channel id', 'your channel name',
                      channelDescription: 'your channel description',
                      largeIcon: DrawableResourceAndroidBitmap("logo"),
                      icon: "ic_launcher",
                      playSound: true,
                      sound: RawResourceAndroidNotificationSound('bell_sound')),
                ),
                androidAllowWhileIdle: true,
                uiLocalNotificationDateInterpretation:
                    UILocalNotificationDateInterpretation.absoluteTime,
              );
              return id;
            } catch (e) {
              log("Error at zonedScheduleNotification----------------------------$e");
              if (e ==
                  "Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime'") {
                Fluttertoast.showToast(msg: "Select future date");
              }
              return -1;
            }
          }