可重现:didRegisterForRemoteNotificationsWithDeviceToken 随机停止被调用

Reproducible: didRegisterForRemoteNotificationsWithDeviceToken randomly stopped being called

didRegisterForRemoteNotificationsWithDeviceToken 工作几周后突然停止调用了。

我可以在 a fresh Xcode project (.zip) 中重现该问题,所以我几乎可以肯定这是一个 signing/provisioning 配置文件问题。令牌在 AppStore 版本中按预期返回,但当 运行 从 Xcode 11.3 在 iPhone 和 iOS 13.

我已经通读了所有相关问题(和 these steps),但没有解决问题。

这是我试过的:

  1. 我用的是真机iPhone,不是模拟器
  2. 已尝试使用蜂窝网络和 WiFi
  3. 创建了新的捆绑 ID
  4. 已检查应用标识符是否明确(无通配符)
  5. 清除了 Keychain.app 和 ~/Library/MobileDevice/Provisioning 配置文件/
  6. 中的所有证书
  7. 使用开发者帐户重新注册设备
  8. 已从 developer.apple.com
  9. 中删除并重新创建开发证书
  10. 多次重新生成、下载、重新安装开发证书
  11. 在 Xcode
  12. 中尝试了手动和“自动管理签名”的上述方法
  13. 已确认配置文件包含 aps-environment 条目 (screenshot)
  14. 向 capabilities/entitlements
  15. 添加了“后台模式:通知”
  16. 尝试删除通知和后台功能然后重新添加它们
  17. 已确认其他 AppStore 应用程序可以向设备发送通知
  18. 已检查 APNS server 已启动
  19. 在上述所有更改之间多次重启设备
  20. 试着每天向推送通知神祈祷

知道可能出了什么问题吗?

代码非常简单:

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Also tried calling after launch finishes.
        // Calling UNUserNotificationCenter.current().requestAuthorization first also never returns a token.
        UIApplication.shared.registerForRemoteNotifications();
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // This method is never called.
        let token = deviceToken.map { String(format: "%02.2hhx", [=10=]) }.joined();
        print(" Received notification token: (\(token))");
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        // This method is never called either (unless I remove aps entitlements).
        print(" Did fail to register for notifications: \(error)");
    }

}

原来是 Apple 端的问题。这是我从开发者支持收到的消息:

Follow-up: 736344716

Hi,

The issue we have recognized is that the timeout for the register call is too long to be useful in normal cases. By the time the registration process decides to timeout and send an error, the app is long gone, and the developer/user has given up. This is going to be addressed in a future iOS release. In the meantime, if this happens again, please make sure your device can access *.push.apple.com on TCP ports 80, 443, 2197, and 5223

I am going to request your TSI to be credited back to your account.