Flutter iOS推送通知不显示问题求助
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 notificationsshould be checked—extra checked options can interfere with push delivery. - Confirm
UIBackgroundModesexists in yourInfo.plistwith theremote-notificationvalue (Xcode adds this automatically when you enable Background Modes, but manual verification never hurts). - Ensure
google-service-info.plistis added directly to the Runner target: go toBuild Phases > Copy Bundle Resourcesand 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 likeReceived remote notificationto confirm if APNs is sending pushes to your device.FirebaseMessaging: Check for errors or warnings about token validation or message processing.
If you seeInvalid tokenlogs, 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

