Using Flutter local notifications for my expiry reminder app, but they’re inconsistent; users often don’t receive alerts reliably

I’m using the Flutter local notification package in my expiry reminder app. However, notifications about product expirations are inconsistent. Sometimes the notifications are delivered as expected, but most of the time, users do not receive them.

I want the functionality to be like alarm apps: when a user sets an expiration time for a product, they should reliably receive a notification at that time.

My main file code

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MyDatabase().copyPasteAssetFileToRoot();
  await MyDatabase().updateDayLeftForAllProducts();
  // await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  await NotificationHelper.init();
  tz.initializeTimeZones();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _requestNotificationPermission();
  }

  Future<void> _requestNotificationPermission() async {
    var status = await Permission.notification.status;
    if (status.isDenied || status.isPermanentlyDenied) {
      status = await Permission.notification.request();
      if (status.isGranted) {
        print("Notification permission granted.");
      } else {
        print("Notification permission denied.");
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => AddItemsProvider()),
          ChangeNotifierProvider(create: (_) => CategoryProvider()),
        ],
        child: Sizer(builder: (context, orientation, screenType) {
          return MaterialApp(
            navigatorKey: navigatorKey,
            debugShowCheckedModeBanner: false,
            title: "Expiry Reminder",
            home: SplashPage(),
          );
        }));
  }
}

These is my Notification file code which is not working proper according to the requirement

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;

class NotificationHelper {
  static final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  static Future<void> onDidReceiveBackgroundNotification(
      NotificationResponse notificationResponse) async {}

  static Future<void> init() async {
    const AndroidInitializationSettings androidInitializationSettings =
        AndroidInitializationSettings('@drawable/launcher_icon');
    const DarwinInitializationSettings iOSInitializationSettings =
        DarwinInitializationSettings();

    const InitializationSettings initializationSettings =
        InitializationSettings(
            android: androidInitializationSettings,
            iOS: iOSInitializationSettings);

    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
      onDidReceiveBackgroundNotificationResponse:
          onDidReceiveBackgroundNotification,
      onDidReceiveNotificationResponse: onDidReceiveBackgroundNotification,
    );

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.requestExactAlarmsPermission();
  }

  static Future<void> scheduleReminderNotifications({
    required String productName,
    required DateTime reminderDate,
    required String reminderTime,
    required String operation,
    required int id,
  }) async {
    List<String> timeParts = reminderTime.split(":");
    final scheduledDate = DateTime(
      reminderDate.year,
      reminderDate.month,
      reminderDate.day,
      int.parse(timeParts[0]),
      int.parse(timeParts[1]),
    );

    if (scheduledDate.isBefore(DateTime.now())) {
      print("Scheduled date must be in the future.");
      return;
    }

    print("Scheduling notification for: $reminderDate for id: $id");

    if (operation == "update") {
      print("Cancel existing notification for id $id");
      await cancelNotification(id);
    }

    await _scheduleNotification(
      id: id,
      title:
          'Attention: ${productName[0].toUpperCase() + productName.substring(1)} Expiration Alert',
      body:
          'Heads up! Your $productName will expire on ${scheduledDate.toString().split(" ")[0]}.',
      scheduledDate: scheduledDate,
    );
  }

  static Future<void> cancelNotification(int id) async {
    await flutterLocalNotificationsPlugin.cancel(id);
  }

  static Future<void> _scheduleNotification({
    required int id,
    required String title,
    required String body,
    required DateTime scheduledDate,
  }) async {
    const NotificationDetails platformChannelSpecifics = NotificationDetails(
      android: AndroidNotificationDetails(
        "channelId",
        "channelName",
        importance: Importance.high,
        priority: Priority.high,
      ),
      iOS: DarwinNotificationDetails(),
    );

    await flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      body,
      tz.TZDateTime.from(scheduledDate, tz.local),
      platformChannelSpecifics,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
      matchDateTimeComponents: DateTimeComponents.dateAndTime,
    );
  }
}

these is how i am adding the notifications

if (buttonText == "Add") {
 if (reminderDate.text.isNotEmpty) {
     NotificationHelper
    .scheduleReminderNotifications(
   productName: productName.text,
  reminderDate: DateTime.parse(
  reminderDate.text),
  reminderTime:
  reminderTimeValue.text,
  operation: buttonText,
  id: index,
 );
} 

//for update purpose
if (reminderDate.text.isNotEmpty) {
                                            NotificationHelper
                                                .scheduleReminderNotifications(
                                              productName: productName.text,
                                              reminderDate: DateTime.parse(
                                                  reminderDate.text),
                                              reminderTime:
                                                  reminderTimeValue.text,
                                              operation: "update",
                                              id: index,
                                            );
                                          }                                        

Please Solve the issue related to the notifications

New contributor

Krishiraj Vansia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

All implementations is correct, for showing zonedSchedule notifications, timezones have to be initialized to latest timezone.

Try initializing tz.initializeTimeZones() in NotificationHelper class instead of main.dart,

import 'package:timezone/data/latest.dart' as tz;

tz.initializeTimeZones();

And replace import of tzone as:

import 'package:timezone/timezone.dart' as tzone;

replace date parameters as tzone.TZDateTime timeToset =
tzone.TZDateTime.from(scheduledDate, tzone.local);

import'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tzone;
import 'package:timezone/data/latest.dart' as tz;
              
class NotificationHelper {
  static final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
              
  static Future<void> onDidReceiveBackgroundNotification(
    NotificationResponse notificationResponse) async {}
              
    static Future<void> init() async {
      tz.initializeTimeZones();
      const AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings('@drawable/launcher_icon');
      const DarwinInitializationSettings iOSInitializationSettings =
      DarwinInitializationSettings();
              
      const InitializationSettings initializationSettings = InitializationSettings(
        android: androidInitializationSettings,
        iOS: iOSInitializationSettings
      );
              
        await flutterLocalNotificationsPlugin.initialize(
          initializationSettings,
          onDidReceiveBackgroundNotificationResponse: onDidReceiveBackgroundNotification,
          onDidReceiveNotificationResponse: onDidReceiveBackgroundNotification,
        );
              
        await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
       ?.requestExactAlarmsPermission();
     }
              
     static Future<void> scheduleReminderNotifications({
       required String productName,
       required DateTime reminderDate,
       required String reminderTime,
       required String operation,
       required int id,
     }) async {
       List<String> timeParts = reminderTime.split(":");
       final scheduledDate = DateTime(
         reminderDate.year,
         reminderDate.month,
         reminderDate.day,
         int.parse(timeParts[0]),
         int.parse(timeParts[1]),
       );
              
       if (scheduledDate.isBefore(DateTime.now())) {
         print("Scheduled date must be in the future.");
         return;
       }
              
       print("Scheduling notification for: $reminderDate for id: $id");
              
       if (operation == "update") {
         print("Cancel existing notification for id $id");
         await cancelNotification(id);
       }
              
       await _scheduleNotification(
         id: id,
         title: 'Attention: ${productName[0].toUpperCase() + productName.substring(1)} Expiration Alert',
         body: 'Heads up! Your $productName will expire on ${scheduledDate.toString().split(" ")[0]}.',
         scheduledDate: scheduledDate,
       );
     }
              
     static Future<void> cancelNotification(int id) async {
       await flutterLocalNotificationsPlugin.cancel(id);
     }
              
     static Future<void> _scheduleNotification({
       required int id,
       required String title,
       required String body,
       required DateTime scheduledDate,
     }) async {
       const NotificationDetails platformChannelSpecifics = NotificationDetails(
         android: AndroidNotificationDetails(
           "channelId",
           "channelName",
           importance: Importance.high,
           priority: Priority.high,
         ),
         iOS: DarwinNotificationDetails(),
       );
       tzone.TZDateTime timeToset = tzone.TZDateTime.from(scheduledDate, tzone.local);
       await flutterLocalNotificationsPlugin.zonedSchedule(
         id,
         title,
         body,
         timeToset,
         platformChannelSpecifics,
         uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
         androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
         matchDateTimeComponents: DateTimeComponents.dateAndTime,
       );
     }
   }

New contributor

Chetan R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật