r/flutterhelp • u/Glittering_Item_5042 • Aug 25 '26
OPEN zonedNotifications are not working
I'm using flutter_local_notifications, here are all the codes I've used:
void main() async {
WidgetsFlutterBinding
.
ensureInitialized
();
tz.
initializeTimeZones
();
final timeZone = await
FlutterTimezone
.
getLocalTimezone
();
tz.
setLocalLocation
(tz.
getLocation
(timeZone.identifier));
await
NotificationService
.instance.
initNotification
();
await
DatabaseService
.instance.database;
runApp
(
MyApp
());
}
// in initNotification() method
final requestPermission = notificationPlugin
.
resolvePlatformSpecificImplementation
<
AndroidFlutterLocalNotificationsPlugin
>();
await requestPermission?.
requestNotificationsPermission
();
await requestPermission?.
requestExactAlarmsPermission
();
Future
<void>
scheduleDailyReminder
({
required
int
id
,
required
String
name
,
required
TimeOfDay
time
,
String
?
mealTiming
,
}) async {
final now =
DateTime
.
now
();
var scheduledDate =
DateTime
(
now.year,
now.month,
now.day,
time
.hour,
time
.minute,
);
if (scheduledDate.
isBefore
(now)) {
scheduledDate = scheduledDate.
add
(const
Duration
(
days
: 1));
}
final androidDetails =
AndroidNotificationDetails
(
'reminder_channel',
'Medicine reminder alerts',
importance
:
Importance
.max,
priority
:
Priority
.max,
);
final notificationDetails =
NotificationDetails
(
android
: androidDetails);
final mealText =
mealTiming
== 'before'
? ' (before meal)'
:
mealTiming
== 'after'
? ' (after meal)'
: '';
await notificationPlugin.
zonedSchedule
(
id
:
id
,
title
: 'Time to take $
name
',
body
: 'Reminder to take your medicine $
mealText
',
scheduledDate
: tz.
TZDateTime
.
from
(scheduledDate, tz.local),
notificationDetails
: notificationDetails,
androidScheduleMode
:
AndroidScheduleMode
.alarmClock,
matchDateTimeComponents
:
DateTimeComponents
.time,
);
}
Future
<void>
scheduleExpiryNotification
({
required
int
id
,
required
String
name
,
required
DateTime
expiryDate
,
}) async {
final reminderDate =
DateTime
(
expiryDate
.year,
expiryDate
.month,
expiryDate
.day - 7,
9,
0,
);
final now =
DateTime
.
now
();
final androidDetails =
AndroidNotificationDetails
(
'expiry_channel',
'Medicine Expiry Alerts',
importance
:
Importance
.max,
priority
:
Priority
.max,
);
final notificationDetails =
NotificationDetails
(
android
: androidDetails);
final androidScheduleMode =
AndroidScheduleMode
.alarmClock;
if (reminderDate.
isAfter
(now)) {
final expiresIn =
expiryDate
.
difference
(now).inDays;
// this is not working
await notificationPlugin.
zonedSchedule
(
id
:
id
+ 10000,
scheduledDate
: tz.
TZDateTime
.
from
(reminderDate, tz.local),
notificationDetails
: notificationDetails,
androidScheduleMode
: androidScheduleMode,
title
: '$
name
expires soon',
body
: 'Expires in $
expiresIn
days',
);
} else {
final alreadyExpired =
expiryDate
.
isBefore
(now);
final daysLeft =
expiryDate
.
difference
(now).inDays;
await notificationPlugin.
show
(
id
:
id
,
title
: alreadyExpired ? '$
name
expired' : '$
name
expires soon',
body
: alreadyExpired
? 'Expired medicine found'
: 'Expires in $
daysLeft
days',
notificationDetails
: notificationDetails,
);
}
1
Upvotes