Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling different notifications on each day Flutter

I am a newbie on Flutter. I am developing an app with notifications where notifications will be shown in different parts of the day.

For instance: (3:25 AM, 12:24 PM, 17:22 PM, 19:52 PM, 21:02 PM).

These times will differ based on the day. Times are stored on DB with their corresponding days. These notifications should be displayed even if the app is terminated and not being used.

Problems:

  1. Get new data on around midnight for the new day. (For example around 00:00 AM my app should reschedule all yesterdays' notifications with new times to inform the user with correct data)
  2. I am using flutter_local_notifications to schedule notification. Sometimes it will not fire a notification (if the scheduled time difference is more than 2 hours). That's is why I am going to schedule the soonest notification (for example if notifications should appear 3:25 AM then I want to schedule notifications 30 minutes before). But I am not sure if it is possible.

What I tried.

  1. background_fetch for syncing time with DB but I could not make it work

  2. flutter_local_notifications for showing notifications

Please consider the case that I want to show the notifications even my app is not running or in the background as well as I want to support both Android and iOS

Any help or suggestions are welcome. Thanks in advance

like image 933
Jasurbek Avatar asked Apr 30 '20 12:04

Jasurbek


People also ask

How do I schedule notifications on Flutter?

After the Initialization, the notification can be scheduled using zonedschedule() method. In that, we should give notification details like Title, description, etc. Android and iOS get their own notification details like android has channels, so we should give channel id, channel name, importance, priority, etc.

How do you set a notification schedule?

On Android, you will need to open Settings. Then choose Sound>Do Not Disturb>Schedules. On this screen, you have several options to set the days and times for Do Not Disturb, choose which apps to apply it to, and set exceptions.

How do you direct push notifications to a specific page in Flutter?

Using FCM Console In this approach, we can send the notification directly from the FCM console. To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field.


1 Answers

For those who are facing the same issue, I managed to solve my problem by the following config

Future<void> initPlatformState() async {
    var config = BackgroundFetchConfig(
      minimumFetchInterval: 15,
      forceAlarmManager: true,
      stopOnTerminate: false,
      startOnBoot: true,
      enableHeadless: true,
      requiresBatteryNotLow: false,
      requiresCharging: false,
      requiresStorageNotLow: false,
      requiresDeviceIdle: false,
      requiredNetworkType: NetworkType.NONE,
    );
    BackgroundFetch.configure(config, _onBackgroundFetch);
  }

Github repo to see the working code. Do not forget to support, if the answer is helpful, upvoting the answer and give a star on github

like image 168
Jasurbek Avatar answered Oct 19 '22 09:10

Jasurbek