How to call a function when the notification arrives? In flutter - 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;
}

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;
}

flutter local notification "The hour of the day, expressed as in a 24-hour clock [0..23]."

i am using flutter local notification, i want to understand the time on it. in the date_time.dart, which is a code file used in flutter local notification, i have found that:
"The hour of the day, expressed as in a 24-hour clock [0..23]."
that means that is i need to create a notification at 8 AM, i should type in code 07.
but the example of flutter local notification, the notification meant to be in 10 AM, but in the code they wrote 10. which means that the range is [1..24], is not it?
The scheduling example code is:
Future<void> _scheduleDailyTenAMNotification() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'daily scheduled notification title',
'daily scheduled notification body',
_nextInstanceOfTenAM(),
const NotificationDetails(
android: AndroidNotificationDetails(
'daily notification channel id',
'daily notification channel name',
'daily notification description'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time);
}
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;
}
Here Next Instance of 10 AM is fetched by passing 10 on the arguments ranging [0..23]
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); //here 10 is for 10:00 AM
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
As the values correspond to 24 Hr format.
0 is for 00:00. and 23 is for 23:00, i.e 12:00 AM and 11:00 PM respectively.
So for setting at next instance of 8:00 AM you would need to pass 8. which will mean 8:00 in 24h and 8:00 AM in 12h format.

How to properly schedule multiple local notifications with Flutter?

I am creating a Habit-Tracker that lets the user specify up to 7 times per day on which he wants to get notified. In case the user wants to do this every day, that would make 49 scheduled Notifications for a single Habit and I feel like there has to be a more efficient way to do it.
I am using flutter_local_notifications
This is my current code :
Future<void> scheduleWeeklyHabitNotifications(Habit habit) async {
for (int weekDay in habit.scheduledWeekDays) {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'Its time for ${habit.title}',
'weekly scheduled notification body',
_nextInstanceOfDayAndTime(weekDay, 20, 30),
const NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'weekly notification channel name',
'weekly notificationdescription'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
}
}
tz.TZDateTime _nextInstanceOfTime(int hour, int minutes) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minutes);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
tz.TZDateTime _nextInstanceOfDayAndTime(int weekDay, int hour, int minute) {
tz.TZDateTime scheduledDate = _nextInstanceOfTime(hour, minute);
while (scheduledDate.weekday != weekDay) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
so this code is working as expected, but, as I said, I feel like creating up to 49 notifications at once, when there is a pending notification limit for iOS of 64 as stated here(and apparently 500 for Samsung) isn't really efficient and I am almost certain I am missing something here.
So what is the proper way to do this ?

Repeating notification - 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