Push notification not register to the app on iOS 13

前端 未结 4 1714
别跟我提以往
别跟我提以往 2021-02-19 03:32

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

相关标签:
4条回答
  • 2021-02-19 04:11

    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.

    0 讨论(0)
  • 2021-02-19 04:20

    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.

    0 讨论(0)
  • 2021-02-19 04:21

    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 dfb16ffffd 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 dfb16ffffd ... 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/

    0 讨论(0)
  • 2021-02-19 04:29

    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.

    0 讨论(0)
提交回复
热议问题