fix(apps): 修复通知点击不显示ReminderOverlay和日历编辑后不刷新问题

- AppDelegate: 只存储payload字段而非整个userInfo字典
- LocalNotificationService: 移除旧的取消/稍后提醒action按钮配置
- ReminderNotificationCallbacks: 添加onNotificationPayloadReceived静态回调
- IOSNotificationPayloadBridge: 添加setPendingPayload方法
- main.dart: 设置onNotificationPayloadReceived触发ReminderOverlay显示,添加WidgetsBindingObserver处理后台恢复
- CalendarEventDetailScreen: 编辑保存后正确传递刷新信号给日视图
This commit is contained in:
qzl
2026-03-20 19:34:06 +08:00
parent fcf98b1142
commit 20b9e70e84
6 changed files with 62 additions and 78 deletions
@@ -21,6 +21,10 @@ class IOSNotificationPayloadBridge {
}
}
Future<void> setPendingPayload(ReminderPayload payload) async {
await _prefs.setString(_key, jsonEncode(payload.toJson()));
}
Future<void> clearPendingPayload() async {
await _prefs.remove(_key);
}
@@ -7,31 +7,15 @@ import 'package:timezone/timezone.dart' as tz;
import 'reminder_notification_callbacks.dart';
import '../../features/calendar/data/models/schedule_item_model.dart';
import '../../features/calendar/reminders/models/reminder_action.dart';
import '../../features/calendar/reminders/models/reminder_payload.dart';
typedef ReminderNotificationActionHandler =
Future<void> Function({
required ReminderAction action,
required ReminderPayload payload,
});
class LocalNotificationService {
static const String _iosCategoryId = 'calendar_reminder_v2';
static const String _actionCancel = 'cancel';
static const String _actionSnooze = 'snooze10m';
final FlutterLocalNotificationsPlugin _plugin;
bool _initialized = false;
ReminderNotificationActionHandler? _actionHandler;
LocalNotificationService({FlutterLocalNotificationsPlugin? plugin})
: _plugin = plugin ?? FlutterLocalNotificationsPlugin();
void bindActionHandler(ReminderNotificationActionHandler handler) {
_actionHandler = handler;
}
Future<void> initialize() async {
if (_initialized) {
return;
@@ -39,25 +23,10 @@ class LocalNotificationService {
tz_data.initializeTimeZones();
const android = AndroidInitializationSettings('@mipmap/ic_launcher');
final ios = DarwinInitializationSettings(
const ios = DarwinInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
notificationCategories: [
DarwinNotificationCategory(
_iosCategoryId,
actions: <DarwinNotificationAction>[
DarwinNotificationAction.plain(_actionCancel, '取消'),
DarwinNotificationAction.plain(
_actionSnooze,
'稍后提醒',
options: <DarwinNotificationActionOption>{
DarwinNotificationActionOption.foreground,
},
),
],
),
],
);
final settings = InitializationSettings(android: android, iOS: ios);
@@ -190,20 +159,11 @@ class LocalNotificationService {
timeoutAfter: 30000,
autoCancel: true,
groupKey: _getGroupKey(fireAt),
actions: <AndroidNotificationAction>[
AndroidNotificationAction(_actionCancel, '取消'),
AndroidNotificationAction(
_actionSnooze,
'稍后提醒',
showsUserInterface: true,
),
],
),
iOS: DarwinNotificationDetails(
presentAlert: true,
presentSound: true,
presentBadge: true,
categoryIdentifier: _iosCategoryId,
threadIdentifier: _getThreadIdentifier(fireAt),
),
);
@@ -341,23 +301,6 @@ class LocalNotificationService {
return;
}
final actionId = response.actionId;
ReminderAction? action;
if (actionId == _actionCancel) {
action = ReminderAction.archive;
} else if (actionId == _actionSnooze) {
action = ReminderAction.snooze10m;
}
if (action == null) {
return;
}
final handler = _actionHandler;
if (handler == null) {
return;
}
await handler(action: action, payload: payload);
ReminderNotificationCallbacks.onNotificationPayloadReceived?.call(payload);
}
}
@@ -5,6 +5,8 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../features/calendar/reminders/models/reminder_payload.dart';
typedef ReminderNotificationResponseHandler =
Future<void> Function(NotificationResponse response);
@@ -13,6 +15,7 @@ class ReminderNotificationCallbacks {
'calendar_reminder_pending_notification_responses_v1';
static ReminderNotificationResponseHandler? _responseHandler;
static Future<void> _pendingStorageLock = Future<void>.value();
static void Function(ReminderPayload)? onNotificationPayloadReceived;
@visibleForTesting
static Future<void> resetForTest() async {