Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule notification everyday for Flutter

I have a prayer-timing App with notifications to be shown for 5 prayers everyday.

For simplicity let's assume there are 5 events(prayers) everyday.

  1. 5:00
  2. 6:00
  3. 7:00
  4. 8:00
  5. 9:00

I want to schedule a notification for every event.

Till here, there is no problem, i can use the Flutter_local_notifications package and use the "daily" method to schedule these 5 notifications daily.

The problem is that these timings change every 2-3 days, so the timings of the notifications also have to be changed in that period.

For now i have written the code in such a way that, whenever the user opens the app, the notifications are scheduled again. This works, but the app freezes for a few seconds on start-up which looks really bad.

Is there any other way to do it?

EDIT : The prayer timings for the whole of next year are calculated in the app, the only problem is to schedule them.

like image 413
Alexander Bourne Avatar asked Aug 11 '26 14:08

Alexander Bourne


2 Answers

As far as I understand your question, I would like to answer it.
Though your prayer timings change, if it has any specific pattern, you could also use Weekly Notification and set each day of the Week with different timings. Something like this:

await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
    0,
    'On Monday Morning',
    'Your Prayer at Monday',
    Day.Monday,
    time,
    platformChannelSpecifics);

Or else, you can send notifications online by Firebase Cloud Messaging (FCM) and there you can schedule it in the console and it will be reflected for all.
FCM Reference: https://firebase.google.com/products/cloud-messaging
For Flutter: https://pub.dev/packages/firebase_messaging
Hope that helps!
#Happy Coding!

like image 186
Shri Hari Avatar answered Aug 14 '26 12:08

Shri Hari


Salam my brother,

I'm also working on a prayer timer app, and I didn't find any method/package that helps.

so what I did is that each time the user opens the app I schedule the prayers for the next 7 days (you can make it the next month or even year), I figured that the user will probably use the app once in a week so I picked week.

Each time I schedule prayers (notifications) I save them in a list in Shared Preferences with a timestamp of the prayer, and before I schedule the next prayers I perform the following:

  1. Fetch the list from Shared Preferences.
  2. Remove passed prayers from the list.
  3. Check if the remaining number of prayers is bigger than 30 (next 6 days).
  4. Schedule new prayers as needed.

And make sure you use unique id for each notification, if you schedule two notifications with the same id then the second one will override the first. I used this method for that:

String getPrayerNotificationId(DateTime time) {
  return DateFormat('yyyyMMddkkmm').format(time);
}

Hope this helps.

like image 41
Othman Shawgan Avatar answered Aug 14 '26 10:08

Othman Shawgan