问题
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.
回答1:
The aforesaid problem could also be resolved by reconnecting wifi or switching between wifi and cellular data.
Moreover, following changes in iOS 13 affected push notification implementation.
Prior to iOS 13 many of us used to do
(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: "")
In iOS 13 apple has changed the implementation of its description method for NSData class. So, it returns
"{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }" // in iOS 13.
Which ended up breaking push notifications implementation for many applications.
From now on, if you need to convert your push notification registration deviceToken into a Base16-encoded / hexadecimal string representation, you should do the following for Swift language
let deviceTokenString = deviceToken.map { String(format: "%02x", $0)
}.joined()
For Objective C, use following code
- (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];
}
I came across a comprehensive article on the given topic https://nshipster.com/apns-device-tokens/
回答2:
As what I understood from trying a lot things about this issue is : sometimes iOS forcing the kind of connection to register device token to apple server.
If you are on WI-FI and didRegisterForRemoteNotificationsWithDeviceToken not called even though you are sure your implemantation of remote notification flow, try switching 3G or 4G. If it is not possible (a test device with no sim) try going to flight mode and activate wireless (which solved our problem).
If you are on 3G - 4G, try switching to wireless connection (There may be a problem if you are using vpn, proxy etc. disable all of them first).
If not other Stackoverflow users suggested deleting the app then restarting the device.
回答3:
If the didRegisterForRemoteNotificationsWithDeviceToken
is not triggering at all, try this.
I tried a lot to fix this issue with my wifi network, but it didnt fixed. So I changed my network to the cellular data and the didRegisterForRemoteNotificationsWithDeviceToken
started triggering again
Also, if you used your internet connection in the MAC to share using USB. Turn it off & connect your IPhone with a normal wifi or mobile data.
回答4:
if u use the testing device (without sim), try to put the sim card and install the app and gave the , it's worked for me.
来源:https://stackoverflow.com/questions/57710188/push-notification-not-register-to-the-app-on-ios-13