I am trying to setup Local Notifications in Flutter app and need the notifications to fire once daily and the notification text to be read from SQLite.
For testing I am trying to have to run these every 2 minutes to see if it picks up the data from SQLite correctly, however it seems to run only once...
await flutterLocalNotificationsPlugin.zonedSchedule(
notificationID,
"Dynamic Data",
await getData(),
_nextInstanceOfTime(initialHour,initialMin),// have defined an initial hr and min to start the notification
const NotificationDetails(
android: AndroidNotificationDetails('1', 'test', 'test')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time
);
tz.TZDateTime _nextInstanceOfTime(int hour, int minutes) {
Duration offsetTime= DateTime.now().timeZoneOffset;
tz.TZDateTime scheduledDate;
tz.TZDateTime currentTime;
if (offsetTime.isNegative) {
scheduledDate = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,hour,minutes)
.add(offsetTime);
currentTime = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,DateTime.now().hour,DateTime.now().minute)
.add(offsetTime);
}
else {
scheduledDate = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,hour,minutes)
.subtract(offsetTime);
currentTime = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,DateTime.now().hour,DateTime.now().minute)
.subtract(offsetTime);
}
if (scheduledDate.isBefore(currentTime)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
scheduledDate = scheduledDate.add(const Duration(minutes: 2));//->>> WANTING TO TEST FIRING IT EVERY 2 MINS
print("next notification:$scheduledDate"); // ->> THIS PRINTS ONLY ONCE
return scheduledDate;
}
getData() gets the random String from SQlite
The print("next notification:$scheduledDate") prints only once and not after every 2 minutes as I need.
Please help !!
Thanks
Related
i can managed to implement time,but year and month note working or should i use some library to run my application in background?
Please Help!
packages i used
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:local_notification_johannas/model.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
Notification class
class NotifyClass {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
NotificInitializer() async {
_confitLocTime();
final AndroidInitializationSettings initSettingAndroid =
AndroidInitializationSettings('send_icon');
final IOSInitializationSettings iniSettingiOS = IOSInitializationSettings();
final InitializationSettings initializationSettings =
InitializationSettings(android: initSettingAndroid, iOS: iniSettingiOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: myOnSelectFuncion);
}
Future myOnSelectFuncion(
String? payload,
) async {
if (payload != null)
print('noti payload :$payload');
else
print('Done');
}
Show Notification Function
Future showNotific({required String title, required String body}) async {
var androidPlatSpecific = new AndroidNotificationDetails(
'channelId', 'channelName',
playSound: true, importance: Importance.max, priority: Priority.high);
var iosPlatSpecific = new IOSNotificationDetails();
var platformSpecific = new NotificationDetails(
android: androidPlatSpecific, iOS: iosPlatSpecific);
await flutterLocalNotificationsPlugin.show(0, title, body, platformSpecific,
payload: 'Default_Sound');
}
This is ScheduledNotification funcion i used.
it only works on minutes or seconds with the same day
even i pass different date.
Future<void> scheduledNotification(
{
required int hour,
required int minutes,
required int day,
required int month,
required int year,
required MyModel obj}) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
obj.id!,
obj.title,
obj.startTime,
_convertTime(year, month, day, hour, minutes),
NotificationDetails(
android: AndroidNotificationDetails('your id', 'your channel'),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
androidAllowWhileIdle: true,
);
}
tz.TZDateTime _convertTime(
int year, int month, int day, int hour, int minutes) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduleDate =
tz.TZDateTime(tz.local, year, month, day, hour, minutes);
if (scheduleDate.isBefore(now)) {
scheduleDate = scheduleDate.add(Duration(days: 1));
print(scheduleDate);
}
return scheduleDate;
}
Future<void> _confitLocTime() async {
tz.initializeTimeZones();
final String timeZone = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(timeZone));
}
}
I am using flutter_local_notifications to show notifications. I am able to see notifications for the same day but as the day changes, I am not able to see any notification.
static void showScheduledNotification(
{int id = 0, String title, String body, String payload, Time time, List<int> days}) async {
print(time.hour.toString() + " : hour time");
print(days.toString() + " : minute time");
_notification.zonedSchedule(
id,
title,
body,
_scheduleWeekly(time, days: days),
await _notificationDetails(),
payload: payload,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
);
}
static tz.TZDateTime _scheduleWeekly(Time time, {List<int> days}) {
tz.TZDateTime scheduledDate = _scheduleDaily(time);
while (!days.contains(scheduledDate.weekday)) {
scheduledDate = scheduledDate.add(Duration(days: 1));
}
return scheduledDate;
}
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.
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;
}
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 ?