flutterandroid-notificationsflutter-local-notificationflutter-notification

flutter local notification does not send notifications


  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();


    // initialization of flutter local notifications plugin
    var initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = DarwinInitializationSettings();
    var initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

    int hour = int.parse(selectedTime.hour.toString());
    int minute = int.parse(selectedTime.minute.toString());
    final alarmId = DateTime.now().millisecondsSinceEpoch.remainder(100000);
    );

    // scheduling local notification
    await flutterLocalNotificationsPlugin.zonedSchedule(
        newAlarm.id,
        'Alarm',
        'Alarm ${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')} için ayarlandı',
        scheduledDate,
        const NotificationDetails(
            iOS: DarwinNotificationDetails(
              sound: 'a_long_cold_sting.wav',
              presentAlert: true,
              presentBadge: true,
              presentSound: true,
            ),
            android: AndroidNotificationDetails(
                'alarm_channel', 'Alarm Channel',
                channelDescription: 'Alarm Channel',
                importance: Importance.high,
                priority: Priority.high,
                enableVibration: true,
                playSound: true,
                sound: RawResourceAndroidNotificationSound('default_alarm'),
                showWhen: false)),
        androidAllowWhileIdle: true,
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
        payload: newAlarm.name,
        matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);

    Navigator.pop(context);
  }

permissions granted and checked that the alarm has been added

The purpose of the code is to add an alarm on a page and process it in the database and trigger the notification when the time comes, but the notification does not appear even though the alarm is added without any problems.


Solution

  • Replace this line

      flutterLocalNotificationsPlugin.initialize(initializationSettings);
    

    with

      await flutterLocalNotificationsPlugin.initialize(initializationSettings);
    

    so that can wait till notifications plugin is initialize.

    you must provide background service to make your idea work well you have 2 of the most efficient packages

    1. workmanager also check this example
    2. flutter_background_service also check the example on the website (example section)