[Flutter/dart] Set regular monthly notifications with local_notification
Overview
A library called flutter_local_notification makes it easy to create regular notifications. However, this library only provides daily and weekly recurring notifications, and there is no method for creating monthly recurring notifications.
So, here's how to send a notification on a fixed day every month.
Method
It's not a very cool solution, but it's pretty good at "creating a one-time notification for months". For example, if you want to send a notification on the 1st of every month, create a notification for January 1st, February 1st, and March 1st, and if the app starts after January 1, create a notification for April 1st, and so on. (You can't create an April 1st notification if the app isn't opened once in three months, but that's not a concern as the app is no longer in use.)
for (int i=0; i<3; i++) {
int _id=i;
DateTime _date= DateTime(2021, i+1, 1, 19, 0);
await flutterLocalNotificationsPlugin.schedule(
_id, 'title', 'content', _date, platformChannelSpecifics);
}
//Methods to craete instances of flutterLocalNotificationsPlugin, platformChannelSpecifics is omitted.
As a caveat, if you do not separate the id, only the last notification will come (one notification with one id), so separate the ids.
Also, the processing will be quite heavy, so create a notification with another isolate. In that case, use flutter_isolate (local_notification is a process that uses platform_channel, so the compute method cannot be used). Click here for how to use flutter_isolate.
there are minor problems such as if you cross the year, you have to change the generation of _date, and if it is the 30th of every month, what to do in February, but as a rough guideline, it is like this. Then apply according to your own application.
Lastly
I think there is a certain demand for sending notifications on a fixed day every month. For example, check the results of this month, or reminder to buy a monthly paper released on the 23rd of every month. If possible, I want the official to respond.
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
Comments