testScheduleNotification static method
Future<NotificationFailure?>
testScheduleNotification(
- FlutterLocalNotificationsPlugin plugin, {
- Duration delay = const Duration(seconds: 10),
Tests scheduling a notification with detailed logging.
Implementation
static Future<NotificationFailure?> testScheduleNotification(
FlutterLocalNotificationsPlugin plugin, {
Duration delay = const Duration(seconds: 10),
}) async {
try {
safeDebugLog('π§ͺ Testing notification scheduling...');
// Run diagnostics first
final Map<String, dynamic> diagnostics = await runDiagnostics(plugin);
safeDebugLog('π Diagnostics: $diagnostics');
// Create test notification
const int testId = 999999;
final DateTime scheduledTime = DateTime.now().add(delay);
final tz.TZDateTime scheduledTZ = tz.TZDateTime.from(
scheduledTime,
tz.local,
);
safeDebugLog('β° Scheduling test notification for: $scheduledTime');
safeDebugLog('β° TZDateTime: $scheduledTZ');
const AndroidNotificationDetails androidDetails =
AndroidNotificationDetails(
'test_channel',
'Test Channel',
channelDescription: 'Test notification channel',
importance: Importance.high,
priority: Priority.high,
category: AndroidNotificationCategory.alarm,
);
const DarwinNotificationDetails iOSDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
);
const NotificationDetails platformChannelSpecifics = NotificationDetails(
android: androidDetails,
iOS: iOSDetails,
macOS: iOSDetails,
);
await plugin.zonedSchedule(
testId,
'π§ͺ Test Scheduled Notification',
'This is a test notification scheduled for ${scheduledTime.toLocal()}',
scheduledTZ,
platformChannelSpecifics,
payload: 'test_scheduled_notification',
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
);
safeDebugLog('β
Test notification scheduled successfully');
// Verify it was scheduled
final List<PendingNotificationRequest> pending = await plugin
.pendingNotificationRequests();
final bool found = pending.any(
(PendingNotificationRequest request) => request.id == testId,
);
if (found) {
safeDebugLog('β
Test notification found in pending list');
} else {
safeDebugLog('β Test notification NOT found in pending list');
return NotificationFailure.scheduling(
details: 'Test notification was not added to pending list',
);
}
return null;
} catch (e, stackTrace) {
safeDebugLog('β Test scheduling failed: $e');
return NotificationFailure.scheduling(
details: 'Test scheduling failed: $e',
stackTrace: stackTrace,
);
}
}