Reputation: 1
so am building an app that sends about 5 local notifications to the user, at an irregular interval (the timings are internally calculated).
First i tried using Scheduled notifications from the package flutter_local_notification and it works quite smoothly, but from what i found there is no direct way that allows me to schedule all 5 notifications at once on the same channel or maybe like trigger a method when the notification is showed so i can reschedule the next notification.
Then i decided to try and use the WorkManager package and mix it with normal notifications from the flutter_local_notification package so i can call a logic that triggers the next notification time and so on, but am facing couple of problems, first the notification that i schedule using workmanager arent so consistent when i close the app, some times they work as intended, and other times it takes much longer than what it was scheduled, or simply only show when i reopen the app, which can be a bit problematic since i need the notifications to pop up on time.
ideally i would love to find a way to only use the flutter_local_notification package because it works quite smoothly and as intended, but it doesnt seem possible for now.
i think i found a somewhat decent solution, suggested by u/autognome in this post Schedule irregular Notifications combining scheduled notifications by the flutter_local_notification and background_fetch that uses headless function implementation that basically will awaken my app in the background about every 15 minutes, providing a short period of background running-time. which will execute the provided callback fun to reschedule the next notification.
finally if you have dealt with irregularly scheduled local notifications or just have a better solution or advice or would like to point our a mistake that i mentioned please go ahead, as i struggled to find a decent guide to do so and i want this to help people after me (and me) to basically not go through the maze i went through.
Ps: if anyone is having the same problem in the future and want some help, hit me up.
Upvotes: 0
Views: 659
Reputation: 109
Irregular Notifications in Flutter (Simple Way):
flutter_local_notifications
.flutter_local_notifications
.This avoids complex background services and offers a good balance for irregular notifications.
In your Flutter code, use the flutter_local_notifications
package to schedule a silent notification with a specific ID (e.g., check_in_notification_id). Set the notification's payload to a value that signals it's a check-in (e.g., "payload": "check_in"
).
Code Example:
// Assuming you have a function to calculate the next notification time
DateTime nextNotificationTime = calculateNextNotificationTime();
// Schedule the silent check-in notification
await FlutterLocalNotificationsPlugin().zonedSchedule(
check_in_notification_id,
"Silent Check-In",
"Waking up the app...",
// Schedule the notification to repeat every 15 minutes
const PeriodicNotificationRepeat(interval: RepeatInterval.everyFifteenMinutes),
AndroidNotificationDetails(...), // Set silent notification details
IOSNotificationDetails(...), // Set silent notification details
payload: {"payload": "check_in"}, // Payload to identify the check-in
);
Placement of Logic:
You don't necessarily need a specific page. Consider creating a dedicated class or service to handle background notification logic (e.g., BackgroundNotificationService). This service can be registered in your main.dart or appropriate initialization code.
Upvotes: 0