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

Python通过Firebase推送通知:Android正常接收iOS无响应问题

Firebase iOS推送收不到的问题排查与修复

核心问题

你的代码仅配置了Android平台的推送参数,完全缺失iOS所需的APNs(Apple Push Notification service)配置,这是iOS设备收不到通知的直接原因。Firebase推送iOS设备时必须通过APNs通道,需显式指定apns相关配置。

修复步骤

  • 添加iOS专属配置:在MulticastMessage中加入apns字段,配置APNs的推送规则,包括通知内容、优先级等。
  • 区分通知类型:iOS对纯数据消息和带UI的通知消息处理逻辑不同:
    • 带UI的通知消息必须包含aps.alert字段,系统才会弹出通知;
    • 纯数据消息需要开启App的「远程通知」后台模式,且需在代码中处理静默推送。
  • 验证设备令牌有效性:不要只看失败计数,打印每个令牌的错误详情,排查是否有令牌过期/无效的情况。
  • 检查APNs证书配置:确保Firebase控制台已上传对应环境(开发/生产)的APNs证书,且证书未过期。
  • 确认设备推送权限:iOS设备需开启App的通知权限,且未开启「专注模式」等屏蔽通知的设置。

修改后的代码示例

def sendPush(notificationTitle, notificationMessage, registration_token):
    message = messaging.MulticastMessage(
        tokens=registration_token,
        data={"channelName": "abc"},
        # 保留原有Android配置
        android=messaging.AndroidConfig(
            priority='high',
            notification=messaging.AndroidNotification(
                title=notificationTitle,
                body=notificationMessage,
                color='#ff0000',
                default_light_settings=True,
                priority="max",
                default_sound=True
            ),
        ),
        # 添加iOS APNs配置
        apns=messaging.APNSConfig(
            payload=messaging.APNSPayload(
                aps=messaging.Aps(
                    alert=messaging.ApsAlert(
                        title=notificationTitle,
                        body=notificationMessage
                    ),
                    sound="default",
                    priority=10  # 高优先级确保后台能收到通知
                )
            )
        )
    )

    response = messaging.send_multicast(message)
    print(response.failure_count, "failure_counts")
    # 打印每个令牌的错误详情,精准排查问题
    for idx, resp in enumerate(response.responses):
        if not resp.success:
            print(f"Token {registration_token[idx]} failed: {resp.exception}")
    print('Successfully sent message:', response.success_count, response.failure_count, len(registration_token))

额外注意事项

  • 生产环境推送需使用生产版APNs证书,且对应正式发布的App(而非Debug包);
  • iOS 13+对推送权限要求更严格,首次启动App时需主动请求用户授权;
  • 若发送静默推送(仅含data无alert),需在Xcode中开启App的「后台模式」-「远程通知」,且代码中实现didReceiveRemoteNotification:fetchCompletionHandler:方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 02:35:19