feat: Implement pending notifications screen and enhance notification handling

This commit is contained in:
2025-08-26 18:15:35 +02:00
parent 2aec59ec35
commit 53dfd92cec
6 changed files with 457 additions and 14 deletions

View File

@@ -431,11 +431,13 @@ class DatabaseHelper {
Future<List<Map<String, dynamic>>> getPendingNotifications() async {
Database db = await database;
return await db.query(
notificationTrackingTable,
where: 'status IN (?, ?)',
whereArgs: ['pending', 'retrying'],
);
return await db.rawQuery('''
SELECT nt.*, s.name as supplementName
FROM $notificationTrackingTable nt
LEFT JOIN $supplementsTable s ON nt.supplementId = s.id
WHERE nt.status IN (?, ?)
ORDER BY nt.scheduledTime ASC
''', ['pending', 'retrying']);
}
Future<void> markNotificationExpired(int notificationId) async {

View File

@@ -148,7 +148,7 @@ class NotificationService {
}
}
void _handleTakeAction(String? payload, int? notificationId) {
Future<void> _handleTakeAction(String? payload, int? notificationId) async {
print('📱 === HANDLING TAKE ACTION ===');
print('📱 Payload received: $payload');
@@ -182,7 +182,7 @@ class NotificationService {
// Mark notification as taken in database (this will cancel any pending retries)
if (notificationId != null) {
print('📱 Marking notification $notificationId as taken');
DatabaseHelper.instance.markNotificationTaken(notificationId);
await DatabaseHelper.instance.markNotificationTaken(notificationId);
// Cancel any pending retry notifications for this notification
_cancelRetryNotifications(notificationId);
@@ -316,16 +316,24 @@ class NotificationService {
final now = DateTime.now();
for (final notification in pendingNotifications) {
final scheduledTime = DateTime.parse(notification['scheduledTime']);
final scheduledTime = DateTime.parse(notification['scheduledTime']).toLocal();
final retryCount = notification['retryCount'] as int;
final lastRetryTime = notification['lastRetryTime'] != null
? DateTime.parse(notification['lastRetryTime'])
? DateTime.parse(notification['lastRetryTime']).toLocal()
: null;
// Check if notification is overdue
final timeSinceScheduled = now.difference(scheduledTime).inMinutes;
final shouldRetry = timeSinceScheduled >= reminderRetryInterval;
print('📱 Checking notification ${notification['notificationId']}:');
print('📱 Scheduled: $scheduledTime (local)');
print('📱 Now: $now');
print('📱 Time since scheduled: $timeSinceScheduled minutes');
print('📱 Retry interval: $reminderRetryInterval minutes');
print('📱 Should retry: $shouldRetry');
print('📱 Retry count: $retryCount / $maxRetryAttempts');
// Check if we haven't exceeded max retry attempts
if (retryCount >= maxRetryAttempts) {
print('📱 Notification ${notification['notificationId']} exceeded max attempts ($maxRetryAttempts)');
@@ -342,7 +350,10 @@ class NotificationService {
}
if (shouldRetry) {
print('📱 ⚡ SCHEDULING RETRY for notification ${notification['notificationId']}');
await _scheduleRetryNotification(notification, retryCount + 1);
} else {
print('📱 ⏸️ NOT READY FOR RETRY: ${notification['notificationId']}');
}
}
} catch (e) {
@@ -381,13 +392,11 @@ class NotificationService {
'take_supplement',
'Take',
showsUserInterface: true,
icon: DrawableResourceAndroidBitmap('@drawable/ic_check'),
),
AndroidNotificationAction(
'snooze_10',
'Snooze 10min',
showsUserInterface: true,
icon: DrawableResourceAndroidBitmap('@drawable/ic_snooze'),
),
],
),