Scheduled flutter local notification is not working; for first time it was working but now it is not workig, I have set more than 10 notification after working a few day, the method for setting the notification, you can see in the following.
AndroidManifest.xml
<manifest xmlns:android=";>
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application>
<receiver android:name="com.dexterous.flutterlocalnotifications.receivers.ActionReceiver" android:exported="true" />
<receiver android:name="com.dexterous.flutterlocalnotifications.receivers.NotificationReceiver" android:exported="true" />
<receiver android:name="com.dexterous.flutterlocalnotifications.receivers.FirebaseReciever" android:exported="true" />
</application>
</manifest>
set_notification.dart
import '../pages/notification_service.dart';
void setNotifications(PrayerTimes prayerTimes) async {
NotificationService().cancelAllNotifications();
NotificationService().scheduleNotification(id:235, title: 'title', body: 'body',
hour: DateTime.now().hour, minute: DateTime.now().minute + 1 );
NotificationService().scheduleNotification(
id: 7,
title: 'title 7',
body: 'body 7',
hour: 16,
minute: 24);
NotificationService().scheduleNotification(
id: 8,
title: 'titel 5',
body: 'bory 8',
hour:15,
minute: 23);
NotificationService().scheduleNotification(
id: 9,
title: 'title 9',
body: 'body 9',
hour: 19,
minute: 45);
NotificationService().scheduleNotification(
id: 10,
title: 'title 10',
body: 'body 10',
hour: 18,
minute: 30);
NotificationService().scheduleNotification(
id: 11,
title: 'title 11',
body: 'body 11',
hour: 22,
minute: 00);
}
I have used this method in home.dart
in initState()
.
notification.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;
class NotificationService {
final FlutterLocalNotificationsPlugin notify =
FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
const InitializationSettings initializationSettings =
InitializationSettings(
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
iOS: DarwinInitializationSettings(),
);
await notify.initialize(initializationSettings);
}
Future<NotificationDetails> notificationDetails() async {
final androidDetails = AndroidNotificationDetails(
'your_channel_id',
'your_channel_name',
channelDescription: 'your_channel_description',
importance: Importance.max,
priority: Priority.high,
playSound: true,
);
final iOSDetails = DarwinNotificationDetails();
return NotificationDetails(android: androidDetails, iOS: iOSDetails);
}
Future showNotification(
{required int id, String? title, String? body, String? payLoad}) async {
return notify.show(id, title, body, await notificationDetails());
}
Future scheduleNotification({
required int id,
required String title,
required String body,
String? payLoad,
required int hour,
required int minute,
}) async {
// Get the current time and initialize Kabul timezone
DateTime now = DateTime.now();
var kabulTimeZone = tz.getLocation('Asia/Kabul');
DateTime scheduledTime = DateTime(now.year, now.month, now.day, hour, minute);
// Ensure the scheduled time is in the future
if (scheduledTime.isBefore(now)) {
scheduledTime = scheduledTime.add(const Duration(days: 1));
}
var scheduledDateTimeKabul = tz.TZDateTime.from(scheduledTime, kabulTimeZone);
// Log the scheduled time and current time
print('Current time: $now');
print('Scheduled time (Kabul): $scheduledDateTimeKabul');
final androidDetails = AndroidNotificationDetails(
'your_channel_id',
'your_channel_name',
channelDescription: 'your_channel_description',
importance: Importance.max,
priority: Priority.high,
playSound: true,
);
final iOSDetails = DarwinNotificationDetails();
final NotificationDetails notificationDetails = NotificationDetails(
android: androidDetails,
iOS: iOSDetails,
);
try {
// Schedule the notification with the correct time zone
await notify.zonedSchedule(
id,
title,
body,
scheduledDateTimeKabul,
await notificationDetails,
androidAllowWhileIdle: true,
payload: payLoad,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
print("Notification scheduled successfully.");
} catch (e) {
print("Error scheduling notification: $e");
}
// Print pending notifications
final List<PendingNotificationRequest> pendingNotifications = await notify.pendingNotificationRequests();
for (var notification in pendingNotifications) {
print('Pending Notification - Title: ${notification.title}, Id: ${notification.id}');
}
}
cancelAllNotifications() {
notify.cancelAll();
}
}
the output if print()
Current time: 2025-02-03 01:34:34.962175
Scheduled time (Kabul): 2025-02-03 01:35:00.000+0430
Notification scheduled successfully.
Pending Notification - Title: title, Id: 235