I schedule user notifications. It works well, but I want to add the hour and minute that come from the user only I have two variables, the hour and the minute, but I do not know where to put the variables. I tried to put them with the year, month, and day, but to no avail. Can I achieve that?
Example:
var schduelTime = tz.TZDateTime(
tz.local,
currentzone.year,
currentzone.month,
currentzone.day,
My_hour,
My_minute,
);
//Code
import 'package:adhdhkar_muslim/virb_uli.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'dart:math';
import 'package:adhdhkar_muslim/virb_uli.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'package:flutter_timezone/flutter_timezone.dart';
import 'virb_uli.dart';
class NotificationService {
static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
static Future init()async{
// ignore: unused_local_variable
InitializationSettings settings = const InitializationSettings(
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
iOS: DarwinInitializationSettings(),
);
flutterLocalNotificationsPlugin.initialize(
settings,
// onDidReceiveBackgroundNotificationResponse:(details) {},
//onDidReceiveNotificationResponse:(details){},
);
}
static void showBasicNotification() async {
const AndroidNotificationDetails android = AndroidNotificationDetails(
'id 3',
"schdulrd Notf",
importance: Importance.max,
priority: Priority.high,
);
NotificationDetails details = const NotificationDetails(
android: android,
);
tz.initializeTimeZones();
var currentzone = tz.TZDateTime.now(tz.local);
var schduelTime = tz.TZDateTime(
tz.local,
currentzone.year,
currentzone.month,
currentzone.day,
19,
30,
);
if (schduelTime.isBefore(currentzone)) {
schduelTime = schduelTime.add(const Duration(days: 1));
//schduelTime = schduelTime.add(const Duration(hours: 1));
}
await flutterLocalNotificationsPlugin.zonedSchedule(
1,
"اذكار الصباح ",
"الله اكبر",
schduelTime
,
details,
uiLocalNotificationDateInterpretation:UILocalNotificationDateInterpretation.absoluteTime);
}
}
I don’t want to add the time manually like in the code. I don’t want that
2
Answers
This code works when the current date is added.
But it gives this error: Exception has occurred. ArgumentError (Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime')
Maybe you can use the ‘add’ method as shown in
But you can replace the duration with a simple equation that subtracts the date and time that the user enters from the DateTime.now().
Like now the time is 05:30 am and wanna it 02:30 pm so subtract it and the result will be 3 hours.
So you added this 3 hours with ‘add’ method.