unknow error while compile the custom code
I WANT TO SCHEDUL THE LOCAL NOTIFIACTION FOR MY ANDROID APP BUT IT'S NOT WORKING
unknow error while compile the custom code
I WANT TO SCHEDUL THE LOCAL NOTIFIACTION FOR MY ANDROID APP BUT IT'S NOT WORKING
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,
);
}