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

Flutter iOS推送通知不显示问题求助

解决Flutter iOS FCM推送通知不显示的问题

Hey there, let's tackle this iOS push notification head-on—since everything works smoothly on Android, we can zero in on iOS-specific tweaks that might be missing. Here are targeted steps to fix this:

1. Align APNs Environment with FCM Configuration

FCM relies on Apple's APNs to deliver pushes to iOS, so environment consistency is key:

  • If you're testing on a physical device via Xcode (development environment), double-check that the APNs key added to Firebase Console is either a development key or a universal key that supports both dev/prod. For TestFlight/App Store builds, use a production key.
  • Update your curl request to explicitly specify the APNs environment and add a title (some iOS versions require this to display notifications):
    curl -X POST --header "Authorization: key=<myAuthKEY>" \
    --Header "Content-Type: application/json" \
    https://fcm.googleapis.com/fcm/send \
    -d "{
      \"to\":\"<myDeviceKey>\",
      \"notification\":{\"title\":\"Test Notification\",\"body\":\"Hello\"},
      \"priority\":10,
      \"apns\":{
        \"headers\":{
          \"apns-push-type\":\"alert\",
          \"apns-environment\":\"development\" # Use "production" for live builds
        }
      }
    }"
    

2. Enhance AppDelegate with Push Callbacks

Your current AppDelegate.m only initializes Firebase, but it's missing critical push-handling methods that let iOS process notifications in background/closed states. Update it with this code:

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
@import Firebase;
@import UserNotifications;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [FIRApp configure];
  [GeneratedPluginRegistrant registerWithRegistry:self];
  
  // Ensure notification permissions are properly set up
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge)
                        completionHandler:^(BOOL granted, NSError * _Nullable error) {
                          if (!error && granted) {
                            [[UIApplication sharedApplication] registerForRemoteNotifications];
                          }
                        }];
  
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// Handle APNs token registration
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeSandbox]; // Use FIRMessagingAPNSTokenTypeProd for production
}

// Handle foreground/background push reception
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}

@end

Make sure to switch FIRMessagingAPNSTokenType based on your environment (Sandbox for dev, Prod for live builds).

3. Verify Xcode Configuration Details

  • Double-check Background Modes in Capabilities: only Remote notifications should be checked—extra checked options can interfere with push delivery.
  • Confirm UIBackgroundModes exists in your Info.plist with the remote-notification value (Xcode adds this automatically when you enable Background Modes, but manual verification never hurts).
  • Ensure google-service-info.plist is added directly to the Runner target: go to Build Phases > Copy Bundle Resources and confirm the file is listed here (not just in your project folder).

4. Dig into Device Console Logs

Connect your iPhone/iPad to Xcode, open the Console (Window > Devices and Simulators > Select your device > Open Console), and search for these keywords:

  • APNs: Look for logs like Received remote notification to confirm if APNs is sending pushes to your device.
  • FirebaseMessaging: Check for errors or warnings about token validation or message processing.
    If you see Invalid token logs, your device token doesn't match the APNs environment—re-fetch the token and retry.

5. Check Dependency Version Compatibility

Your firebase_messaging: ^5.1.6 and Flutter 1.9.1 are older versions, which might have compatibility issues with Xcode 11. If all above steps fail, try upgrading to the latest firebase_messaging version compatible with Flutter 1.9 (e.g., ^6.0.16—verify compatibility first), then re-install dependencies and retest.

One last quick check: Open your device's Settings > [Your App] > Notifications to confirm Allow Notifications is enabled, and options like Lock Screen/Notification Center are toggled on.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:16:06