testScheduleNotification static method

Future<NotificationFailure?> testScheduleNotification(
  1. FlutterLocalNotificationsPlugin plugin, {
  2. 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,
    );
  }
}