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

Firebase Messaging无法发送主题消息,iOS推送通知故障求助

Firebase Cloud Messaging推送问题排查方案

问题概况

使用Firebase Admin Python服务向iOS 18真机发送主题推送,控制台提示发送成功,但设备未收到消息,FCM仪表盘无记录;直接用FCM Token发单推时触发401未授权错误(提示APNS或Web Push服务认证失败)。

客户端已正常生成FCM Token,订阅info主题无报错;服务端通过JSON密钥认证,可正常读写Firebase数据库。

客户端代码(iOS)

class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
    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) { _, _ in }
        application.registerForRemoteNotifications()

        return true
    }

    func application(
        _ application: UIApplication,
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {
        Messaging.messaging().apnsToken = deviceToken
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { }
}

服务端代码(Python)

import firebase_admin
from firebase_admin import messaging, credentials

cred = credentials.Certificate("credsAhmed.json")
firebase_admin.initialize_app(cred)

message = messaging.Message(
    notification=messaging.Notification(
        title = "New Release",
        body = "Nike Book 1 The Nightmare Before Christmas",
    ),
    topic = 'info',
)

response = messaging.send(message)
print('Successfully sent message:', response)

排查步骤

1. 核对APNS认证配置

  • 登录Firebase控制台→项目设置→云消息传递→iOS应用,检查APNs认证方式(证书/令牌)是否有效:
    • 证书认证:确认证书未过期,且与苹果开发者后台的推送证书一致(开发/生产环境匹配);
    • 令牌认证:确认密钥ID、团队ID、Bundle ID完全匹配苹果开发者后台信息,且密钥未被撤销。
  • 若使用开发环境测试,需确保APNs配置为开发环境,生产环境同理。

2. 确认主题订阅有效性

  • 在Firebase控制台→云消息传递→主题,查看info主题的订阅设备数,确认设备实际完成订阅;
  • 客户端订阅时添加错误日志,排查隐藏问题:
    Messaging.messaging().subscribe(toTopic: "info") { error in
        if let err = error {
            print("主题订阅失败: \(err)")
        } else {
            print("成功订阅info主题")
        }
    }
    

3. 调整推送消息结构(适配iOS 18)

iOS 18对APNs推送格式要求更严格,需显式指定APNs参数:

message = messaging.Message(
    notification=messaging.Notification(
        title="New Release",
        body="Nike Book 1 The Nightmare Before Christmas",
    ),
    topic='info',
    apns=messaging.APNSConfig(
        headers={"apns-push-type": "alert", "apns-environment": "development"}, # 生产环境改为"production"
        payload=messaging.APNSPayload(
            aps=messaging.Aps(
                alert=messaging.ApsAlert(
                    title="New Release",
                    body="Nike Book 1 The Nightmare Before Christmas"
                ),
                sound="default",
                badge=1
            )
        )
    )
)

4. 验证服务端权限

  • 检查服务账号的IAM权限,确保拥有Cloud Messaging Sender或Firebase Admin SDK Administrator Service Agent权限;
  • 确认权限已正确绑定到服务账号,无权限继承或生效延迟问题。

5. 检查设备通知设置

  • 在iOS设备的「设置→通知」中,确认目标应用已开启允许通知,且通知样式(横幅/提醒)未被禁用;
  • 检查设备是否处于静音、专注模式等可能屏蔽通知的状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 10:14:53