feat: 实现日历提醒 in-app fallback 机制及通知服务重构

This commit is contained in:
zl-q
2026-03-20 01:30:34 +08:00
parent 7fd536e976
commit d574128815
55 changed files with 4565 additions and 647 deletions
@@ -0,0 +1,142 @@
import 'dart:io';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:social_app/core/notifications/reminder_notification_callbacks.dart';
void main() {
setUp(() async {
SharedPreferences.setMockInitialValues({});
await ReminderNotificationCallbacks.resetForTest();
});
test('contains top-level vm entry-point background callback', () async {
final source = await File(
'lib/core/notifications/reminder_notification_callbacks.dart',
).readAsString();
expect(source, contains("@pragma('vm:entry-point')"));
expect(source, contains('Future<void> reminderNotificationTapBackground('));
});
test(
'dispatches foreground and background responses to bound handler',
() async {
final handledIds = <int?>[];
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
handledIds.add(response.id);
});
await ReminderNotificationCallbacks.onForegroundResponse(
NotificationResponse(
notificationResponseType:
NotificationResponseType.selectedNotificationAction,
id: 10,
),
);
await reminderNotificationTapBackground(
NotificationResponse(
notificationResponseType:
NotificationResponseType.selectedNotificationAction,
id: 20,
),
);
expect(handledIds, <int?>[10, 20]);
},
);
test(
'queues background response when handler is unbound and drains later',
() async {
await reminderNotificationTapBackground(
NotificationResponse(
notificationResponseType:
NotificationResponseType.selectedNotificationAction,
id: 99,
),
);
final handledIds = <int?>[];
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
handledIds.add(response.id);
});
expect(handledIds, <int?>[99]);
},
);
test(
'queues foreground response when handler is unbound and drains later',
() async {
await ReminderNotificationCallbacks.onForegroundResponse(
NotificationResponse(
notificationResponseType:
NotificationResponseType.selectedNotificationAction,
id: 55,
),
);
final handledIds = <int?>[];
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
handledIds.add(response.id);
});
expect(handledIds, <int?>[55]);
},
);
test('failed pending item stays queued for next bind retry', () async {
await reminderNotificationTapBackground(
NotificationResponse(
notificationResponseType:
NotificationResponseType.selectedNotificationAction,
id: 77,
),
);
var firstAttempt = true;
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
if (firstAttempt) {
firstAttempt = false;
throw Exception('temporary failure');
}
});
final handledIds = <int?>[];
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
handledIds.add(response.id);
});
expect(handledIds, <int?>[77]);
});
test(
'background handler failure while bound is enqueued for retry',
() async {
var firstAttempt = true;
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
if (firstAttempt) {
firstAttempt = false;
throw Exception('temporary failure');
}
});
await reminderNotificationTapBackground(
NotificationResponse(
notificationResponseType:
NotificationResponseType.selectedNotificationAction,
id: 123,
),
);
final handledIds = <int?>[];
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
handledIds.add(response.id);
});
expect(handledIds, <int?>[123]);
},
);
}