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

iOS端Firebase推送通知失效(400错误)求助

排查iOS端Firebase推送通知失效问题

已实现的服务端代码

Firebase客户端初始化

$client = new Google_Client();

$client->useApplicationDefaultCredentials(); 

// Alternatively, provide the JSON authentication file directly.
$client->setAuthConfig('firebase/project_service.json');

$client->addScope('https://www.googleapis.com/auth/firebase.messaging');

$httpClient = $client->authorize();

//Firebase project ID
$project = 'myProject';

推送Payload

Array
(
    [message] => Array
        (
            [notification] => Array
                (
                    [title] => Test Notification
                    [body] => Test body
                )

            [token] => 4136ADAC16BEAAC8E4A57CBFAB5859E93E5A9A9DB3CE12D047225C7648B74BE4
        )

)

发送请求代码

$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);

iOS端Token获取代码

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
    ALog(@"Push notification token registration success.");

    const char *data = [deviceToken bytes];
    NSMutableString *token = [NSMutableString string];
    
    for (NSUInteger i = 0; i < [deviceToken length]; i++) {
        [token appendFormat:@"%02.2hhX", data[i]];
    }
    
    if (getTokenCallbackId != nil) {
        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:token];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.getTokenCallbackId];

        getTokenCallbackId = nil;
    }
}

问题现象

  • Android端推送正常
  • Firebase控制台创建推送活动可给iOS设备正常推送,但使用「发送测试消息」功能输入Token时无法推送
  • 相同Payload搭配Android设备Token可正常工作,搭配iOS设备Token则失效
  • 已在Firebase云消息面板中添加APN密钥,完成所有前置配置,尝试过Firebase官方指南的必要步骤仍未解决

排查方向

  • Token格式不匹配:FCM要求iOS设备Token为小写十六进制字符串,当前代码用%02.2hhX生成的是大写格式,可能导致匹配失败。修改为%02.2hhx(小写x)生成Token,或在上传服务端前转成小写。
  • Payload缺少iOS专属配置:仅使用notification字段在iOS App后台状态下可能无法触发通知,需补充apns字段配置。示例:
    $message = [
        'message' => [
            'notification' => [
                'title' => 'Test Notification',
                'body' => 'Test body'
            ],
            'token' => 'iOS_DEVICE_TOKEN',
            'apns' => [
                'payload' => [
                    'aps' => [
                        'sound' => 'default',
                        'badge' => 1
                    ]
                ]
            ]
        ]
    ];
    
  • Token环境不匹配:确认iOS Token来自开发环境还是生产环境,Firebase控制台测试消息需选择对应环境;服务端调用时可通过apns.headers.apns-environment指定:
    'apns' => [
        'headers' => [
            'apns-push-type' => 'alert',
            'apns-environment' => 'development' // 或 production
        ]
    ]
    
  • 检查FCM响应错误:打印服务端请求的响应内容$response->getBody()->getContents(),查看是否有明确错误提示(如InvalidRegistration表示Token无效)。
  • Token有效性问题:用户卸载重装App、设备注销推送权限都会导致Token失效,需确认当前使用的Token是最新且有效的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 05:25:44