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

Flutter iOS真机无法接收Azure Notification Hub推送通知问题

Flutter iOS真机无法通过Azure Notification Hub接收APNS推送的排查方案

基于你的问题背景——生产环境真机可接收其他第三方推送,但Azure Notification Hub的APNS推送失败,以下是针对性的排查步骤和解决方案:

一、验证Azure Notification Hub的APNS环境配置

  • 登录Azure Portal,进入目标Notification Hub,导航至Settings > Apple (APNS)
    • 确认环境选择为Production(你的应用处于生产环境,不能使用Sandbox环境配置)
    • 若使用Token Authentication:
      • 核对Key ID、Team ID与Apple Developer后台完全一致
      • 确认上传的.p8密钥文件已开启APNS权限,且Bundle ID精确匹配应用配置
    • 若使用Certificate Authentication:
      • 确保证书为生产环境推送证书(而非开发证书),且未过期、包含Push Notification权限

二、检查设备注册逻辑的正确性

Azure Notification Hub针对APNS推送,必须使用APNS Token作为设备标识,而非device_info_plus获取的设备ID:

  1. 客户端注册时,需将Firebase Messaging获取的APNS Token转换为标准十六进制字符串(去除Data类型自带的空格和尖括号)
  2. Flutter端注册示例代码:
final String? apnsToken = await FirebaseMessaging.instance.getAPNSToken();
if (apnsToken != null) {
  // 向Azure Notification Hub注册设备
  await NotificationHub.registerDevice(apnsToken, ['your-target-tags']);
}
  1. 在Azure Portal的Devices页面,确认设备已成功注册,且平台标记为APNS

三、排查推送Payload的有效性

  1. 绕过Azure,用APNS直接测试工具(如apns2)发送标准生产环境Payload,验证设备能否正常接收:
{
  "aps": {
    "alert": {
      "title": "测试标题",
      "body": "测试内容"
    },
    "sound": "default"
  },
  "data": {
    "body": "测试内容",
    "title": "测试标题",
    "code": "test",
    "alert": "test-alert",
    "extra_params": {},
    "type": "notification"
  }
}
  1. 检查你拼接的Payload是否存在语法错误:
    • 确保extraParametersStringBuilder.ToString()生成合法JSON对象(空值时应为{}而非空字符串)
    • 验证拼接后的完整JSON格式符合APNS生产环境规范,无多余逗号或未闭合引号

四、修正AppDelegate的推送处理逻辑

  1. 完善didReceiveRemoteNotification方法,添加日志并确保非FCM推送被正确处理:
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("收到远程通知:\(userInfo)")
    // 仅处理FCM消息,其他推送直接完成回调
    if Messaging.messaging().isFirebaseMessage(userInfo) {
        Messaging.messaging().appDidReceiveMessage(userInfo)
    }
    completionHandler(UIBackgroundFetchResult.newData)
}
  1. 确认willPresent方法中,非Freshchat通知的展示配置正确(确保应用前台也能收到通知):
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let freshchatPlugin = FreshchatSdkPlugin()
    if freshchatPlugin.isFreshchatNotification(notification.request.content.userInfo) {
        freshchatPlugin.handlePushNotification(notification.request.content.userInfo)
    } else {
        // 开启前台展示通知的所有权限
        completionHandler([.alert, .sound, .badge])
    }
}
  1. 可暂时注释Firebase相关推送处理代码,测试Azure推送是否能被接收,排除FCM的干扰

五、利用Azure日志定位问题

在Azure Portal的Notification Hub中开启Diagnostics Settings,收集推送日志:

  • 查看推送请求的状态码和错误信息(如InvalidToken表示Token无效,DeviceNotFound表示设备未注册)
  • 根据日志提示针对性修正配置或注册逻辑

内容的提问来源于stack exchange,提问作者Adel N. Daniel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:07:44