You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS端Flutter应用前台运行时无法显示本地通知问题咨询

Fixing Foreground Notifications for flutter_local_notifications on iOS

Hey there! Let's figure out why your iOS notifications aren't popping up when your app is in the foreground—your core confusion makes total sense, and the fix is straightforward once you know where to look.

Your Core Question Answered

Your initial understanding was a bit off: the flutter_local_notifications plugin does NOT automatically configure foreground notification display for iOS by default. The docs mention "default settings for foreground display" might be misleading—you actually need to explicitly set presentation options to make this work.

Why This Happens

iOS 10+ requires apps to explicitly define how notifications should behave when the app is active. By default, the IOSInitializationSettings uses an empty presentationOptions array, which means notifications get silently delivered without showing up to the user when the app is in the foreground.

Modified Working Code

Here's your updated code with the critical fixes, plus a few extra steps to ensure everything works as expected:

Future<void> showNotification() async {
  final notificationPlugin = FlutterLocalNotificationsPlugin();
  
  // First, request iOS notification permissions (critical for notifications to work at all)
  await notificationPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
    alert: true,
    badge: true,
    sound: true,
  );

  const androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
  // Configure iOS initialization to allow foreground notifications
  const iOSSettings = IOSInitializationSettings(
    presentationOptions: [
      IOSNotificationPresentationOption.alert, // Show the notification alert
      IOSNotificationPresentationOption.sound, // Play notification sound
      IOSNotificationPresentationOption.badge, // Update app badge count
    ],
  );
  final initializationSettings = InitializationSettings(
    android: androidSettings,
    iOS: iOSSettings,
  );
  await notificationPlugin.initialize(
    initializationSettings,
    onSelectNotification: (payload) async {},
  );

  const androidNotificationDetails = AndroidNotificationDetails(
    'foo_id',
    'Foo Name',
    'Foo description ',
    importance: Importance.max,
    priority: Priority.defaultPriority,
    showWhen: false,
  );
  // Add iOS-specific notification details (optional but recommended for consistency)
  const iOSNotificationDetails = IOSNotificationDetails();

  await notificationPlugin.show(
    1211,
    'Notification title',
    'Notification body',
    NotificationDetails(
      android: androidNotificationDetails,
      iOS: iOSNotificationDetails, // Don't forget this line!
    ),
    payload: 'item x',
  );
}

Key Changes Explained

  • presentationOptions Addition: This tells iOS to display the alert, play sound, and update the badge even when your app is in the foreground. Without these, notifications stay hidden.
  • Permission Request: iOS requires explicit user permission for notifications—adding this step ensures you don't run into silent failures if the user hasn't allowed notifications yet.
  • IOSNotificationDetails: While not strictly required, including this ensures your notification configuration is complete across both platforms.

Extra Checks to Ensure Success

  1. Update your Info.plist with permission descriptions (required for iOS):
    <key>NSAlertUsageDescription</key>
    <string>We need to show notifications to alert you of important updates</string>
    <key>NSBadgeUsageDescription</key>
    <string>We use badges to let you know when you have new notifications</string>
    <key>NSSoundUsageDescription</key>
    <string>We play sounds to alert you to new notifications</string>
    
  2. If you're using remote notifications (not just local), enable background modes in Info.plist:
    <key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>
    

After making these changes, your iOS app should display notifications properly whether it's in the foreground or background.

内容的提问来源于stack exchange,提问作者iDecode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:12:43