Scheduling different notifications on each day Flutter
Asked Answered
C

2

8

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

Cruel answered 30/4, 2020 at 12:21 Comment(0)
C
7

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

Cruel answered 31/5, 2020 at 12:21 Comment(5)
I have tried same with background fetch but unable to do so, but your example is working perfectly fine, thanks. Just to inform add this in build.gradle for background fetch maven { // [required] background_fetch url "${project(':background_fetch').projectDir}/libs" }Arda
I would also really appreciate if it is possible for you to connect with me personally and explained bit in detail, thanks with heart!Arda
What was unclear for you? Did you follow all steps in the readme file of the plugins?Cruel
Terrible solution. This will fetch every 15 minutes at MINIMUM. That is unacceptable for a real time chat app. What, someone sends me a chat message and im supposed to wait 15 minutes till i get a notification?Breastbone
@Breastbone you need to have a look at server push notifications for that purpose, it is local push notifications, you need the understand the difference between them and implement them accordinglyCruel
F
0

If you are using PHP to connect to your database then you can actually use PHP to schedule the time and then send the notification to flutter, just like I did in my application, my application is about sending a notification when the delivery order status change to users, so I created a table in the database and convert it to an API which I call in my flutter application using work manager package and flutter local notification package, would upload the video to youtube this weekend and post the link here for you to see... or can you kindly tell me about your database, what are you using to connect etc

this is the youtube video https://youtu.be/b6CTeqJnmN0

Flagellum answered 8/5, 2020 at 17:58 Comment(4)
Things are stored in my local database. If your solution could be suitable in my situation then I would go for Firebase Push Notification. But I want to show notification even if the phone does not have an internet connection. Your solution will fail at this point. But anyway thanks for sharing your way of solving the problem. Any other idea?Cruel
Yes have been thinking on what you said and this what came to my head, I don't know about the local storage or how you want to connect to get the time, but what I think is first try to check the youtube I added to my reply on how to use work manager, I think calling your dart code in work manager should work, just like an example I gave in the video, but yours would be connecting to local storage, also even if you not connecting the database and you want notification like every 1hr or 35 or any min you like, it the same step, just write a dart code that would get the current time andFlagellum
calculate the Minn you want then call the notification function to display. I hope you got me, check the youtube video I uploaded.Flagellum
I saw your video on youtube. I can address two issues. First, the code you wrote on video will not work in Android Xiaomi, OPPO,... if the application cleared from recent app tabs. You should have a workaround on the issue Second It will not work properly on iOS when the App is killed or terminated. But I want it should work on both platform flawlesslyCruel

© 2022 - 2024 — McMap. All rights reserved.