Push notification not register to the app on iOS 13

Hal*_*MAZ 7 xcode ios ios13

I build my app and I put a breakpoint in didRegisterForRemoteNotificationsWithDeviceToken but it's not triggered. It works fine on other versions of iOS.

Is this a bug in iOS 13 or did I miss something new in iOS 13?

I use Xcode Beta 6 and iOS 13 beta 8.

小智 6

在iOS 13之前,我们很多人曾经做过

(deviceToken as NSData).description 
// Used to return as follows

"<965b251c 6cb1926d e3cb366f dfb16ddd e6b9086a 8a3cac9e 5f857679 376eab7C>"

let tokenData = deviceToken as NSData
let token = tokenData.description

let token = "\(deviceToken)".replacingOccurrences(of: " ", with: "")
                            .replacingOccurrences(of: "<", with: "")
                            .replacingOccurrences(of: ">", with: "")
Run Code Online (Sandbox Code Playgroud)

在iOS 13中,苹果更改了NSData类的描述方法的实现。所以,它返回

"{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }" // in iOS 13.
Run Code Online (Sandbox Code Playgroud)

最终破坏了许多应用程序的推送通知实现。

从现在开始,如果您需要将推送通知注册deviceToken转换为Base16编码的/十六进制字符串表示形式,则应对Swift语言执行以下操作

let deviceTokenString = deviceToken.map { String(format: "%02x", $0) 
}.joined()
Run Code Online (Sandbox Code Playgroud)

对于目标C,请使用以下代码

- (NSString *)hexadecimalStringFromData:(NSData *)deviceToken {
  NSUInteger dataLength = deviceToken.length;
  if (dataLength == 0) {
    return nil;
  }

  const unsigned char *dataBuffer = (const unsigned char *)deviceToken.bytes;
  NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];
  for (NSInteger index = 0; index < dataLength; ++index) {
    [hexString appendFormat:@"%02x", dataBuffer[index]];
  }
  return [hexString copy];
}
Run Code Online (Sandbox Code Playgroud)

我遇到了有关给定主题https://nshipster.com/apns-device-tokens/的详尽文章

  • 感谢您的建议,但要求不同。为什么 iOS13 上不再调用 didRegisterForRemoteNotificationsWithDeviceToken ?我现在也遇到同样的问题 (4认同)
  • 这与所提出的问题完全无关。如果 didRegisterForRemoteNotificationsWithDeviceToken 从未被调用,则传递给该调用的数据格式无关。 (2认同)