Repeating notification - Flutter

Viewed 1983

How can I schedule a monthly or weekly notification in flutter? The purpose is for it to act as a reminder for the user about something...

here is my code

void showMonthlyAtDayTime() async {
  //var scheduledNotificationDateTime = DateTime.now().add(Duration(seconds: 10));
  var time = Time(12, 6, 0);
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'alarm_notif',
    'Scheduled notification', // name of the notif channel
    'A scheduled notification', // description of the notif channel
    icon: 'question',
    largeIcon: DrawableResourceAndroidBitmap('question'),
  );

  var iOSPlatformChannelSpecifics = IOSNotificationDetails( 
      presentAlert: true,
      presentBadge: true,
      presentSound: true);
  var platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.showMonthlyAtDayTime(
      0, 
      'Title',
      'Body',
      Day.Tuesday,
      time, 
      platformChannelSpecifics);
}

The code has (red) underline beneath the showMonthlyAtDayTime in the

'wait flutterLocalNotificationsPlugin.showMonthlyAtDayTime' line

and the 'Day.Tuesday' line...

How should I modify the above code? so that the user will get notification once per each month of every year about something.

1 Answers

Some methods have been removed in the latest versions.

This is the code I found to repeat a notification weekly using flutter_local_notifications.

Future<void> _scheduleWeeklyTenAMNotification() async {
await notificationsPlugin.zonedSchedule(
    0,
    'weekly scheduled notification title',
    'weekly scheduled notification body',
    _nextInstanceOfTenAM(),
    const NotificationDetails(
      android: AndroidNotificationDetails(
          'weekly notification channel id',
          'weekly notification channel name',
          'weekly notificationdescription'),
    ),
    androidAllowWhileIdle: true,
    uiLocalNotificationDateInterpretation:
    UILocalNotificationDateInterpretation.absoluteTime,
    matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
  }
  tz.TZDateTime _nextInstanceOfTenAM() {
    final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
    tz.TZDateTime scheduledDate =
    tz.TZDateTime(tz.local, now.year, now.month, now.day, 10);
    if (scheduledDate.isBefore(now)) {
      scheduledDate = scheduledDate.add(const Duration(days: 1));
    }
    return scheduledDate;
  }

this code is copied from the example app provided by https://pub.dev/packages/flutter_local_notifications

here is the git hub link click Here

Related