Luca
Luca
Top Bug Hunter
 · Founder | CTO

Scheduled Notifications on Android with flutter_local_notifications

Custom Code
Resolved

Hi,

I currently use flutter_local_notifications to show the user a notification (alarm) that he has previously defined.

Under iOS everything works perfectly, under Android I get the following error message when setting the time (save):

exact_alarms_not_permitted, Exact alarms are not permitted, null, null

My code looks like this.

Future<void> scheduleNotification(
    String title,
    String content,
    String time,
    int notificationId,
    String? deeplink,
    String channelId,
    String channelName,
    String channelDescription
    /* Change the type to String for the time parameter */
    ) async {
  await requestNotificationPermissions();
  /* Parse the time string into a DateTime object */
  DateTime parsedTime = DateFormat.Hm().parse(time);

  /* Initialize the timezone database */
  tzdata.initializeTimeZones();

  /* Schedule notification based on the provided time */
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  /* Initialize the Android-specific settings for the notification */
  var androidSettings = AndroidNotificationDetails(
    channelId,
    channelName,
    channelDescription: channelDescription,
    importance: Importance.max,
    priority: Priority.high,
    playSound: true,
    // sound: ''
    icon:
        '@mipmap/ic_launcher', /* Replace with your small icon name without the extension */
  );

  /* Initialize the iOS-specific settings for the notification */
  const DarwinNotificationDetails iOSPlatformChannelSpecifics =
      DarwinNotificationDetails(threadIdentifier: 'thread_id');

  /* Initialize the notification details */
  var notificationDetails = NotificationDetails(
    android: androidSettings,
    iOS: iOSPlatformChannelSpecifics,
  );

  /* Get the device's timezone */
  var deviceTimeZone = tz.getLocation('Europe/Berlin');

  /* Convert the provided time to the device's timezone */
  var scheduledTime = tz.TZDateTime.from(parsedTime, deviceTimeZone);

  /* Schedule the notification */
  await flutterLocalNotificationsPlugin.zonedSchedule(
    notificationId,
    title,
    content,
    scheduledTime,
    notificationDetails,
    androidAllowWhileIdle: true,
    androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
    matchDateTimeComponents: DateTimeComponents.time,
    // payload:
    //"{'click_action': 'FLUTTER_NOTIFICATION_CLICK', 'notificationid': ${notificationId}, 'screen': 'mPPushLandingpage'}",
    payload: deeplink,
  );
}

Since I'm not very familiar with local notifications, it would be great if someone had more knowledge and could help me.

What have you tried so far?

reading documentations, stackoverflow, etc.

Did you check FlutterFlow's Documentation for this topic?
Yes
5
23 replies