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

iOS生产环境Flutter应用Firebase后台推送接收异常求助

问题排查与修复方案

1. 调整APNs消息优先级

你当前设置的apns-priority为5,这是低优先级,iOS在后台状态下会延迟推送甚至直接丢弃这类消息。生产环境要触发后台推送,必须设置为高优先级10:

{
  "to" : "/topics/782",
  "notification" : {
      "body" : "body is here",
      "title": "Title of Your Notification",
      "click_action": "FLUTTER_NOTIFICATION_CLICK",
      "channel_id": "example.de",
      "sound":"true"
  },
  "apns": {
        "headers": {
            "apns-priority": "10"
        }
      }
}

2. 修复AppDelegate的通知代理配置

你的AppDelegate存在两处核心问题:重复设置通知代理、未实现iOS10+通知处理的关键方法,导致后台推送无法正常展示。修改后的代码如下:

import UIKit
import Flutter
import FirebaseCore
import FirebaseMessaging
import FirebaseAuth
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {

    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    
    // 统一设置通知代理,移除冗余的版本判断
    UNUserNotificationCenter.current().delegate = self
    
    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(
      options: authOptions,
      completionHandler: { _, _ in }
    )
    application.registerForRemoteNotifications()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
  
  override func application(_ application: UIApplication,
  didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
       Messaging.messaging().apnsToken = deviceToken
       #if DEVELOPMENT
            Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
        #else
            Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
        #endif
       super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
     }
  
  // 处理前台通知展示
  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      completionHandler([.alert, .badge, .sound])
  }
  
  // 处理通知点击回调
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
      completionHandler()
  }
}

3. 验证APNs生产证书配置

  • 确认Firebase控制台中,iOS应用的APNs认证使用的是生产环境证书(而非开发证书)
  • 证书绑定的Bundle ID必须与TestFlight版本的App Bundle ID完全一致
  • 检查Apple开发者后台中,证书未过期、已正确关联目标App ID

4. 确认通知通道配置

iOS10+要求通知必须关联有效通道,确保Flutter端已创建对应channel_id为example.de的通知通道:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

void initNotificationChannel() async {
  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'example.de', // 与推送消息中的channel_id保持一致
    'Example Notification Channel',
    description: 'Channel for app notifications',
    importance: Importance.max,
  );
  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
}

5. 检查TestFlight设备权限

确保TestFlight安装的App已获得用户授权:打开系统设置→对应App→通知,确认“允许通知”已开启,且App的“后台App刷新”权限未关闭

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:17:48