I'm encountering an issue with scheduled notifications in my Flutter application using the flutter_local_notifications package. While immediate notifications function correctly, scheduled notifications do not appear as expected. There are no errors or warnings in the debug console.
flutter_local_notifications Version: ^19.0.0
Platform: Android 10
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_timezone/flutter_timezone.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
class NotificationService {
final notificationsPlugin = FlutterLocalNotificationsPlugin();
bool _isInitialized = false;
bool get isInitialized => _isInitialized;
// Initialize
Future<void> initNotification() async {
if (_isInitialized) return;
// Initialize timezone
tz.initializeTimeZones();
final String currentTimeZone = await FlutterTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(currentTimeZone));
print("Device Timezone: $currentTimeZone");
const initSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
const initSettings = InitializationSettings(android: initSettingsAndroid);
await notificationsPlugin.initialize(initSettings);
_isInitialized = true;
}
// Notification Detail
NotificationDetails notificationDetails() {
return NotificationDetails(
android: AndroidNotificationDetails(
'daily_channel_id',
'Daily Notifications',
channelDescription: 'Daily notifications for the app',
importance: Importance.max,
priority: Priority.high,
),
);
}
// Show Notifications
Future<void> showNotifications({
int id = 0,
String? title,
String? body,
}) async {
return notificationsPlugin.show(
id,
title,
body,
notificationDetails(),
);
}
// Schedule a notification at a specified time (hour: 0-23, min: 0-59)
Future<void> scheduleNotification({
int id = 1,
required String title,
required String body,
required int hour,
required int minute,
}) async {
// Get the current date/time in device's local timezone
final now = tz.TZDateTime.now(tz.local);
print("Current Time (TZ): $now");
// Create a date time for today at the specified hour and minute
var scheduledDate = tz.TZDateTime(
tz.local,
now.year,
now.month,
now.day,
hour,
minute,
);
print("Scheduled Time (Before Adjustment): $scheduledDate");
// Ensure scheduled time is in the future
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(Duration(days: 1));
print("Scheduled Time Adjusted to Next Day: $scheduledDate");
}
// Schedule the notification
await notificationsPlugin.zonedSchedule(
id,
title,
body,
scheduledDate,
notificationDetails(),
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
matchDateTimeComponents: DateTimeComponents.dateAndTime,
);
print("Final Scheduled Time: $scheduledDate");
}
// Cancel all notifications
Future<void> cancelAllNotifications() async {
await notificationsPlugin.cancelAll();
}
}
Trigger immediate notifications using the
showNotifications method, which works as intended.
Despite calling the scheduleNotification
method and observing the printed scheduled times in the console, the
notifications do not appear at the specified times.
Confirmed that the device's time zone is correctly identified and set
using the flutter_timezone and timezone packages.
Inserted print statements to verify the current time and the
calculated scheduled time. The logs indicate that the scheduled times
are computed correctly.
Verified that the application has the necessary permissions to
display notifications. Since the target device is running Android 10,
no additional permissions are required for scheduling exact alarms.
Checked and ensured that battery optimization settings are not
restricting the application's background processes.