2

I use flutter local notification package to schedule notification in my application but it only schedule one notification at a time if i set two notification on different time then only one notification is displayed here is my code of scheduled notification function:

Future<void> _configureLocalTimeZone() async {
     tz.initializeTimeZones();
     final String timeZone = await FlutterNativeTimezone.getLocalTimezone();
     tz.setLocalLocation(tz.getLocation(timeZone));
   }
   /// Set right date and time for notifications
   tz.TZDateTime _convertTime(int hour, int minutes) {
     final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
     tz.TZDateTime scheduletime = tz.TZDateTime(
       tz.local,
       now.year,
       now.month,
       now.day,
       hour,
       minutes,
     );
     if (scheduletime.isBefore(now)) {
       scheduletime = scheduletime.add(const Duration(days: 1));
     }
     print(scheduletime);
     return scheduletime;
   }
   Future scheduled_notification() async{
     AndroidNotificationDetails android_notification=AndroidNotificationDetails('your channel id', 'Your channel name',
       channelDescription: 'your channel description',
       playSound: true,
       priority: Priority.high,
       importance: Importance.max,
     );
     NotificationDetails notificationDetails=NotificationDetails(android: android_notification);
     await flutterLocalNotificationsPlugin.zonedSchedule(
         0 ,
         'Dose',
         'Ayesha apky puff ka time hogya hai...',
        _convertTime(hours_24,minutes) ,
     notificationDetails,
         uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
         matchDateTimeComponents: DateTimeComponents.time,
         androidAllowWhileIdle: true
     );
   }

and here is my where i call the function:

 ElevatedButton(
          onPressed:  (){
           scheduled_notification();
}
)

1 Answers1

2

for the notifications to be scheduled at same time, you need to mention each notification's id, in your case you have notificationId=0, if you schedule more than 1 notifications with this id, previous one be overridden by new one.

await flutterLocalNotificationsPlugin.zonedSchedule(
         0 ,
         'Dose',
         'Ayesha apky puff ka time hogya hai...',
        _convertTime(hours_24,minutes) ,
     notificationDetails,
         uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
         matchDateTimeComponents: DateTimeComponents.time,
         androidAllowWhileIdle: true
     );


await flutterLocalNotificationsPlugin.zonedSchedule(
         1 ,
         'Dose',
         'Ayesha apky puff ka time hogya hai...',
        _convertTime(hours_24,minutes) ,
     notificationDetails,
         uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
         matchDateTimeComponents: DateTimeComponents.time,
         androidAllowWhileIdle: true
     );
Nalin Nishant
  • 716
  • 1
  • 5
  • 20