2

I've been working on the Flutter project and now I am trying to schedule notification by using flutter_local_notification. I've integrated that dependency into my project and it works fine. Now I want to schedule a notification to be alerted on the app like 2 days before, and then 4 hours before, and then 30 minutes before.

For example, my app has an event called A, and then I want to schedule that event A to send a notification for the user like 1 day before, and then 1 hour before, and then 30 minutes before the event A starts. So it will send a notification 3 times.

For my scenario: In order to send a notification we have to pass the notification id. Let's consider event A has id '123'. Do I have to schedule that notification 3 times with the same notification id '123'?

Let's check the code I have done in order to send a notification:

Future<void> showNotification({
  @required int id,
  @required DateTime dateTime,
  String title,
  String body,
}) async {
  final TimeZone timeZone = TimeZone();
  final String tzName = await timeZone.getTimeZoneName();
  final tz.Location location = await timeZone.getLocation(tzName);
  final scheduledDate = tz.TZDateTime(
    location,
    dateTime.year,
    dateTime.month,
    dateTime.day,
    dateTime.hour,
    dateTime.minute,
    dateTime.second,
  );
  log('------------> Schedule date $scheduledDate');
  final AndroidNotificationDetails androidChannelSpecifics =
     AndroidNotificationDetails(
    '$id',
   'NAME $id',
   'DESCRIPTION $id',
   importance: Importance.max,
   priority: Priority.high,
   ledOnMs: 1000,
   ledOffMs: 500,
 );
 final IOSNotificationDetails iosNotificationSpecifics =
     IOSNotificationDetails();
 final notificationDetails = NotificationDetails(
     iOS: iosNotificationSpecifics, android: androidChannelSpecifics);
 await flutterLocalNotificationsPlugin.zonedSchedule(
  id,
  title,
  body,
  scheduledDate,
  notificationDetails,
  matchDateTimeComponents: DateTimeComponents.time,
  uiLocalNotificationDateInterpretation: null,
  androidAllowWhileIdle: true,
 );
}

For notification initialization like configuring in AndroidManifest.xml, I also have done it already.

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
final BehaviorSubject<ReceivedNotification> didReceivedNotificationSub =
    BehaviorSubject<ReceivedNotification>();
InitializationSettings initializationSettings;
LocalNotificationiHelper._() {
  init();
}

Future<void> init() async {
  flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  tz.initializeTimeZones();
  if (Platform.isIOS) {
   _requestIOSPermission();
  }
  initialPlatformSpecifics();
}

void initialPlatformSpecifics() {
  const AndroidInitializationSettings initializeAndroidSetting =
      AndroidInitializationSettings('@mipmap/ic_launcher');
  final IOSInitializationSettings initializeIOSSetting =
      IOSInitializationSettings(
       requestAlertPermission: true,
       requestBadgePermission: true,
      requestSoundPermission: false,
     onDidReceiveLocalNotification:
      (int id, String title, String body, String payload) async {
      final ReceivedNotification receivedNotification = ReceivedNotification(
         id: id, title: title, body: body, payload: payload);
         didReceivedNotificationSub.add(receivedNotification);
      },
   );
   initializationSettings = InitializationSettings(
      android: initializeAndroidSetting, iOS: initializeIOSSetting);
}

void _requestIOSPermission() {
   flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
         IOSFlutterLocalNotificationsPlugin>()
     .requestPermissions(
      alert: false,
      badge: true,
      sound: true,
    );
 }

You might've noticed the class TimeZone(), it is just another class that I created to get the current time zone of the user.

Here is the class for TimeZone()

class TimeZone {
   factory TimeZone() => _this ?? TimeZone._();

   TimeZone._() {
     initializeTimeZones();
   }
   static TimeZone _this;

  Future<String> getTimeZoneName() async =>
     FlutterNativeTimezone.getLocalTimezone();

  Future<t.Location> getLocation([String timeZoneName]) async {
     if (timeZoneName == null || timeZoneName.isEmpty) {
       timeZoneName = await getTimeZoneName();
     }
   return t.getLocation(timeZoneName);
  }
}

For example, event A with id 123 will start at 2020-11-19 16:00:00. So I want to know that should we schedule that notification 3 times in order for it to be sent like 1 day before, 3 hours before, and 30 minutes before the event start??

Visal Sambo
  • 1,270
  • 1
  • 19
  • 33

1 Answers1

0

This depends on how you'd like to set the interval. One way that you can schedule recurring notifications using flutter_local_notifications is by utilizing flutterLocalNotificationsPlugin.periodicallyShow() where you can set intervals like RepeatInterval.everyMinute - usage can be found in the docs.

Omatt
  • 8,564
  • 2
  • 42
  • 144