11// ignore_for_file: depend_on_referenced_packages
22
3+ import 'dart:convert' ;
4+
5+ import 'package:crypto/crypto.dart' ;
36import 'package:flutter/foundation.dart' ;
47import 'package:flutter_local_notifications/flutter_local_notifications.dart' ;
58import 'package:timezone/data/latest.dart' as tz;
@@ -23,22 +26,33 @@ class NotificationService {
2326 await _flutterLocalNotificationsPlugin.initialize (initializationSettings);
2427 }
2528
26- void sendNotification (DateTime dtb, String task) async {
29+ // Function to create a unique notification ID
30+ int calculateNotificationId (
31+ DateTime scheduledTime, String taskname, int ? taskid) {
32+ String combinedString = '${scheduledTime .toIso8601String ()}$taskname ' ;
33+
34+ // Calculate SHA-256 hash
35+ var sha2561 = sha256.convert (utf8.encode (combinedString));
36+
37+ // Convert the first 8 characters of the hash to an integer
38+ int notificationId =
39+ int .parse (sha2561.toString ().substring (0 , 8 ), radix: 16 ) % 2147483647 ;
40+ if (taskid != null ) {
41+ notificationId = (notificationId + taskid) % 2147483647 ;
42+ }
43+
44+ return notificationId;
45+ }
46+
47+ void sendNotification (DateTime dtb, String taskname, int ? taskid) async {
2748 DateTime dateTime = DateTime .now ();
2849 tz.initializeTimeZones ();
2950 if (kDebugMode) {
3051 print ("date and time are:-$dateTime " );
3152 print ("date and time are:-$dtb " );
3253 }
3354 final tz.TZDateTime scheduledAt =
34- tz.TZDateTime .from (dtb.add (const Duration (minutes: 1 )), tz.local);
35- final tz.TZDateTime scheduledAt1 = tz.TZDateTime .local (dateTime.year,
36- dateTime.month, dateTime.day, dateTime.hour, dateTime.minute);
37- // print("date and time are:-" + dateTime.toString());
38- if (kDebugMode) {
39- print ("dtb is :-$scheduledAt " );
40- print ("date and time are scheduled2:-$scheduledAt1 " );
41- }
55+ tz.TZDateTime .from (dtb.add (const Duration (minutes: 0 )), tz.local);
4256
4357 AndroidNotificationDetails androidNotificationDetails =
4458 const AndroidNotificationDetails ('channelId' , 'TaskReminder' ,
@@ -49,18 +63,37 @@ class NotificationService {
4963 NotificationDetails notificationDetails =
5064 NotificationDetails (android: androidNotificationDetails);
5165
52- _flutterLocalNotificationsPlugin.zonedSchedule (
53- scheduledAt.day * 100 + scheduledAt.hour * 10 + scheduledAt.minute,
54- 'Task Warrior Reminder' ,
55- 'Hey! Your task of $task is still pending' ,
56- scheduledAt,
57- notificationDetails,
58- uiLocalNotificationDateInterpretation:
59- UILocalNotificationDateInterpretation .absoluteTime,
60- // ignore: deprecated_member_use
61- androidAllowWhileIdle: true );
66+ // Generate a unique notification ID based on the scheduled time and task name
67+ int notificationId = calculateNotificationId (dtb, taskname, taskid);
68+
69+ await _flutterLocalNotificationsPlugin
70+ .zonedSchedule (
71+ notificationId,
72+ 'Task Warrior Reminder' ,
73+ 'Hey! Your task of $taskname is still pending' ,
74+ scheduledAt,
75+ notificationDetails,
76+ uiLocalNotificationDateInterpretation:
77+ UILocalNotificationDateInterpretation .absoluteTime,
78+ // ignore: deprecated_member_use
79+ androidAllowWhileIdle: true )
80+ .then ((value) {
81+ if (kDebugMode) {
82+ print ('Notification scheduled successfully' );
83+ }
84+ }).catchError ((error) {
85+ if (kDebugMode) {
86+ print ('Error scheduling notification: $error ' );
87+ }
88+ });
89+
6290 if (kDebugMode) {
6391 print (scheduledAt.day * 100 + scheduledAt.hour * 10 + scheduledAt.minute);
6492 }
6593 }
94+
95+ // Delete previously scheduled notification with a specific ID
96+ void cancelNotification (int notificationId) async {
97+ await _flutterLocalNotificationsPlugin.cancel (notificationId);
98+ }
6699}
0 commit comments