Scheduling local notifications

Custom Code

unknow error while compile the custom code

I WANT TO SCHEDUL THE LOCAL NOTIFIACTION FOR MY ANDROID APP BUT IT'S NOT WORKING

What have you tried so far?

flutter_local_notifications: ^17.1.1

permission_handler: ^11.3.0

timezone: ^0.9.2

This is my code:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
import 'dart:io';

// ─── Extra imports for Android notifications ─────────────────────────────────
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

// -----------------------------------------------------------------------------
// Custom Action: scheduleLocalNotification (Android only)
// -----------------------------------------------------------------------------

Future<void> scheduleLocalNotification(
  int notificationId, // must match if you later cancel/update

  String title,

  String body,

  DateTime scheduledDate,
) async {
  // ── 1. Initialize the plugin ────────────────────────────────────────────────

  final plugin = FlutterLocalNotificationsPlugin();

  const androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');

  await plugin.initialize(const InitializationSettings(android: androidInit));

  tz.initializeTimeZones();

  // ── 2. Ask for POST_NOTIFICATIONS permission on Android 13+ ───────────────

  if (Platform.isAndroid) {
    if (await Permission.notification.isDenied) {
      await Permission.notification.request();

      // If still denied, you might want to show a dialog directing the user to Settings β†’ Notifications.
    }
  }

  // ── 3. Convert DateTime β†’ tz-aware timestamp in the device’s local zone ─────

  final tzDate = tz.TZDateTime.from(scheduledDate, tz.local);

  // ── 4. Schedule the notification ───────────────────────────────────────────

  await plugin.zonedSchedule(
    notificationId,

    title,

    body,

    tzDate,

    const NotificationDetails(
      android: AndroidNotificationDetails(
        'tasks', // channel ID (create once, then reuse)

        'Task reminders', // channel name (visible in system settings)

        importance: Importance.max,

        priority: Priority.high,
      ),
    ),

    androidAllowWhileIdle: true,

    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.wallClockTime,

    matchDateTimeComponents: DateTimeComponents.dateAndTime,
  );
}
Did you check FlutterFlow's Documentation for this topic?
No
1
2 replies