Repeating notification - Flutter - flutter

How can I schedule a monthly or weekly notification in flutter? The purpose is for it to act as a reminder for the user about something...
here is my code
void showMonthlyAtDayTime() async {
//var scheduledNotificationDateTime = DateTime.now().add(Duration(seconds: 10));
var time = Time(12, 6, 0);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'alarm_notif',
'Scheduled notification', // name of the notif channel
'A scheduled notification', // description of the notif channel
icon: 'question',
largeIcon: DrawableResourceAndroidBitmap('question'),
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true);
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.showMonthlyAtDayTime(
0,
'Title',
'Body',
Day.Tuesday,
time,
platformChannelSpecifics);
}
The code has (red) underline beneath the showMonthlyAtDayTime in the
'wait flutterLocalNotificationsPlugin.showMonthlyAtDayTime' line
and the 'Day.Tuesday' line...
How should I modify the above code? so that the user will get notification once per each month of every year about something.

Some methods have been removed in the latest versions.
This is the code I found to repeat a notification weekly using flutter_local_notifications.
Future<void> _scheduleWeeklyTenAMNotification() async {
await notificationsPlugin.zonedSchedule(
0,
'weekly scheduled notification title',
'weekly scheduled notification body',
_nextInstanceOfTenAM(),
const NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'weekly notification channel name',
'weekly notificationdescription'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
}
tz.TZDateTime _nextInstanceOfTenAM() {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, 10);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
this code is copied from the example app provided by https://pub.dev/packages/flutter_local_notifications
here is the git hub link click Here

Related

Combine periodically and zoneschedule in flutter local notification,

I am trying to use a local notification flutter and try to combine periodicallyshow and zonescheduled,
Can anyone give a link or example reference?
Thank you
PERIODICALLYSHOW CODE
void scheduleNotification(String title, String body) async {
AndroidNotificationDetails androidNotificationDetails =
const AndroidNotificationDetails(
'channelIs',
'channelName',
importance: Importance.max,
priority: Priority.high,
);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
);
await _flutterLocalNotificationsPlugin.periodicallyShow(
0,
title,
body,
RepeatInterval.daily,//NOTE - repeat daily datetime(now) when press
notificationDetails,
);
}
ZONESCHEDULLED
static Future scheduleNotification({
int id = 0,
String? title,
String? body,
String? payload,
required DateTime scheduledDate,
}) async =>
_notification.zonedSchedule(
id,
title,
body,
_scheduleDaily(const Time(07, 30)),//NOTE - SCHEDULE SPECIFIC TIME
await _notificationDetails(),
payload: payload,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
static tz.TZDateTime _scheduleDaily(Time time) {
final now = tz.TZDateTime.now(tz.local);
final scheduledDate = tz.TZDateTime(
tz.local,
now.year,
now.month,
now.day,
time.hour,
time.minute,
time.second,
);
return scheduledDate.isBefore(now)
? scheduledDate.add(const Duration(days: 1))
: scheduledDate;
}
When I run the function, it will run the notification repeatedly at the specific at 10:00 that I have determined

Flutter schedule local notification does not work

I am trying to create a notification at a certain time but it is not working. The code does not throw any error but no notification is displayed on the device.
I am using flutter_local_notifications
Code that generates the notification:
Future<void> showNotification(
int hour, int id, String title, String body) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
_convertTime(hour),
const NotificationDetails(
android: AndroidNotificationDetails('main_channel', 'Main Channel',
channelDescription: "ashwin",
importance: Importance.max,
priority: Priority.max),
iOS: IOSNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
}
_converTime function code:
TZDateTime _convertTime(int hour) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduleDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, 47);
return scheduleDate;
}
The result of the function is correct and is as follows: 2022-07-14 12:47:00.000Z
However, if instead of using this function, I change it to
Future<void> showNotification(
int hour, int id, String title, String body) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
// _convertTime(hour), // this does not work
tz.TZDateTime.now(tz.local).add(Duration(seconds: 1)), // this work
const NotificationDetails(
android: AndroidNotificationDetails('main_channel', 'Main Channel',
channelDescription: "ashwin",
importance: Importance.max,
priority: Priority.max),
// iOS details
iOS: IOSNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
}
I don't understand what could be happening.
I appreciate any help in advance.
try this one, remove some properties if you don't want them...
Future zonedScheduleNotification(String note, DateTime date, occ) async {
// tz.TZDateTime.parse(location, formattedString)
int id = math.Random().nextInt(10000);
log(date.toString());
log(tz.TZDateTime.parse(tz.getLocation("Asia/Kolkata"), date.toString())
.toString());
try {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
occ,
note,
tz.TZDateTime.parse(tz.getLocation("Asia/Kolkata"), date.toString()),
NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description',
largeIcon: DrawableResourceAndroidBitmap("logo"),
icon: "ic_launcher",
playSound: true,
sound: RawResourceAndroidNotificationSound('bell_sound')),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
return id;
} catch (e) {
log("Error at zonedScheduleNotification----------------------------$e");
if (e ==
"Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime'") {
Fluttertoast.showToast(msg: "Select future date");
}
return -1;
}
}
Try this again.
tz.TZDateTime _convertTime(int hour) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduleDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, 47);
return scheduleDate;
}

How to call a function when the notification arrives? In flutter

I am using the plugin flutter_local_notifications 6.0.0, where the user schedules a specific time to send the database.db file
I was able to carry out the daily notification with the respective programming but I don't know where to call the function to execute it, even if the app is closed.
use the following code for local notification programming, but I need to call a function to send the .db file.
Future<void> _scheduleDailyTenAMNotification(int hour, int minute) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'Send Backup','',
_nextInstanceOfTenAM(hour, minute),
const NotificationDetails(
android: AndroidNotificationDetails(
'indeterminate progress channel',
'indeterminate progress channel',
'indeterminate progress channel description',
channelShowBadge: false,
importance: Importance.max,
priority: Priority.high,
onlyAlertOnce: true,
showProgress: true,
indeterminate: true)
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time);
}
tz.TZDateTime _nextInstanceOfTenAM(int hour, int minute) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}

How can I run multiple notifications on flutter with specified time for each one?

I use a notifications that repeat every hour with different id and channel , but i got just the first one evert time and other blocked and sometimes lastone only.
this is my notification function
Future<void> _scheduleNotification(id,duration,channelname,channelid,soundandroid,soundios,message,title) async {
var vibrationPattern = Int64List(4);
vibrationPattern[0] = 0;
vibrationPattern[1] = 1000;
vibrationPattern[2] = 5000;
vibrationPattern[3] = 2000;
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
channelid,
channelname,
'your other channel description',
icon: '#mipmap/ic_launcher',
importance: Importance.Max,
priority: Priority.High,
ongoing: true,
autoCancel: false,
sound: RawResourceAndroidNotificationSound(soundandroid),
largeIcon: DrawableResourceAndroidBitmap('#mipmap/ic_launcher'),
vibrationPattern: vibrationPattern,
enableLights: true,
color: const Color.fromARGB(255, 255, 0, 0),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ledOffMs: 500);
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(sound:soundios);
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(
id,
title,
message,
RepeatInterval.Hourly,
platformChannelSpecifics,
);
}
and I call this function to be executed
Future<void> smartnotificationcall(channelname,id)
async{
if(channelname=="clean"){ Duration(seconds:5); return _scheduleNotification(1,20,'clean',"clean","clean","clean.wav",'test','test');}
if(channelname=="sport"){ Duration(seconds:20); return _scheduleNotification(2,30,"sport","sport","sport","sport.wav",'test','test');}
if(channelname=="eat"){ Duration(seconds:40); return _scheduleNotification(3,30,"eat","eat","eat","eat.wav",'test','test');}
if(channelname=="getout"){ Duration(seconds:50); return _scheduleNotification(4,30,"getout","getout","getout","getout.wav",'test','test');
}
}
Which platform do you use? Android uses AlarmManager to schedule repeating notifications and they are inexact and this is documented Android behaviour.

Local notification flutter

can anyone show me with code that how can I schedule the notification in flutter using local notification plugin.
Tried the example in git repository buy it doesn't work for me, although a normal notification is working but how can i schedule it for a specific time like a reminder?
From the plugin sample code if you wanna schedule a notification you have to use a code like that:
/// Schedules a notification that specifies a different icon, sound and vibration pattern
Future _scheduleNotification() async {
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
var vibrationPattern = new Int64List(4);
vibrationPattern[0] = 0;
vibrationPattern[1] = 1000;
vibrationPattern[2] = 5000;
vibrationPattern[3] = 2000;
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description',
icon: 'secondary_icon',
sound: 'slow_spring_board',
largeIcon: 'sample_large_icon',
largeIconBitmapSource: BitmapSource.Drawable,
vibrationPattern: vibrationPattern,
color: const Color.fromARGB(255, 255, 0, 0));
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(sound: "slow_spring_board.aiff");
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
The part you must focus is that:
// Schedule a notification in 5 secs from now
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
I suggest you to clone the plugin repo and try its example if you are not sure about the native projects setup to do in order to have your notifications to be shown.
/// IMPORTANT: running the following code on its own won't work as
there is setup required for each platform head project.
/// Please
download the complete example app from the GitHub repository where all
the setup has been done
const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails('repeating channel id','repeating channel name', 'repeating description');
const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title', 'repeating body', RepeatInterval.everyMinute, platformChannelSpecifics, androidAllowWhileIdle: true);
you can use periodicallyShow flutter_local_notifications