'Flutter Local Notifications runs the Callback function on every hot restart

I am currently working on a Habit Tracker app in Flutter. I want to send a notification to the user at 11PM everyday mentioning the number of Habits they have left for the day, if there is any

My Problem: The callback function runs every time I hot restart the app, or in cases where the device is not connected, it runs every time I close the app and open it again. This happens until the end of the day (12:00 AM). What is the fix to this? How do I make it send the notification only once a day. This feels like a simple fix but I'm not sure what I'm doing wrong.

My Code:

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

void scheduleNotification() async
{
  print("scheduleNotification() function Ran");
  var scheduledNotificationDateTime = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 23, 00, 00);

  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'habit_notif',
    'habit_notif',
    channelDescription: 'Channel for Habit notification',
    icon: 'applogocircle2',
    largeIcon: DrawableResourceAndroidBitmap('applogoyellowsquare'),
  );

  var iOSPlatformChannelSpecifics = IOSNotificationDetails(
      presentAlert: true,
      presentBadge: true,
      presentSound: true
  );
  var platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics
  );

  List<Map<String, dynamic>> queryRows = await DatabaseHelper.instance.queryAllValues();

  int completed = queryRows[0]["todayCompleted"];
  int total = queryRows[0]["todayTotal"];

  if(completed != total)
  {
    await flutterLocalNotificationsPlugin.schedule(
      0,
      'You haven\'t completed all your habits for today!',
      "You have ${total-completed} habits left",
      scheduledNotificationDateTime,
      platformChannelSpecifics,
      androidAllowWhileIdle: true,
    );
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await UserSimplePreferences.init();
  runApp(const MyApp());

  WidgetsFlutterBinding.ensureInitialized();

  var initializationSettingsAndroid = AndroidInitializationSettings('applogo');
  var initializationSettingsIOS = IOSInitializationSettings(
    requestAlertPermission: true,
    requestBadgePermission: true,
    requestSoundPermission: true,
    onDidReceiveLocalNotification: (int id, String? title, String? body, String? payload) async {}
  );

  var initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS
  );

  await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
      onSelectNotification: (String? payload) async {
        if (payload != null) {
          debugPrint('Notification payload: ' + payload);
        }
      }
  );

  scheduleNotification();
  );
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source