Does iOS 13 has new way of getting device notification token?

后端 未结 7 1652
自闭症患者
自闭症患者 2020-12-03 04:55

So my friend got this email from OneSignal

Due to a change that may occur as part of the upcoming iOS 13 release, you must update to the latest versio

相关标签:
7条回答
  • 2020-12-03 05:05

    You can have look in the below code as I was also stuck on this problem. Here is the code by which you can get the device token in below iOS 13 and above.

        NSString *str = [NSString stringWithFormat:@"%@", devTokendata]; // devTokendata is NSData
        str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@"<" withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@">" withString:@""];
        if (@available(iOS 13, *)) {
            str = [self hexadecimalStringFromData:devToken];
        NSLog(@"APNS Token: %@",str);
    }
    
    -(NSString *)deviceTokenFromData:(NSData *)data
    {
        NSUInteger dataLength = data.length;
        if (dataLength == 0) {
            return nil;
        }
        const unsigned char *dataBuffer = (const unsigned char *)data.bytes;
        NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];
        for (int i = 0; i < dataLength; ++i) {
            [hexString appendFormat:@"%02x", dataBuffer[i]];
        }
        return [hexString copy];
    }
    
    0 讨论(0)
  • 2020-12-03 05:07

    The way you do it is fine and it should continue to work on iOS 13. But some developers do it like this. To convert Data into base-16 strings, they call description, which returns something like

    <124686a5 556a72ca d808f572 00c323b9 3eff9285 92445590 3225757d b83997ba>
    

    And then they trim < and > and remove spaces.

    On iOS 13 the description called on token data returns something like

    { length = 32, bytes = 0xd3d997af 967d1f43 b405374a 13394d2f ... 28f10282 14af515f }
    

    Which obviously makes this way broken.

    Another example of wrong implementation (already edited to include correct implementation as well).

    Some more examples might be found in this thread.

    0 讨论(0)
  • 2020-12-03 05:12

    The same code for Swift 5 but bit shorter variant. Verified at iOS 13.

    func getStringFrom(token:NSData) -> String {
        return token.reduce("") { $0 + String(format: "%02.2hhx", $1) }
    }
    
    0 讨论(0)
  • 2020-12-03 05:16
    func getStringFrom(deviceToken: Data) -> String {
        var token = ""
        for i in 0..<deviceToken.count {
            token += String(format: "%02.2hhx", arguments: [deviceToken[i]])
        }
        return token
    }
    
    0 讨论(0)
  • 2020-12-03 05:17

    Correctly capture iOS 13 Device Token in Xamarin.iOS

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        //DeviceToken = Regex.Replace(deviceToken.ToString(), "[^0-9a-zA-Z]+", "");
        //Replace the above line whick worked up to iOS12 with the code below:
        byte[] bytes = deviceToken.ToArray<byte>();
        string[] hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
        DeviceToken = string.Join(string.Empty, hexArray);
    }
    

    Here is what's going on here:

    1. First we have to grab all the bytes in the device token by calling the ToArray() method on it.
    2. Once we have the bytes which is an array of bytes or, byte[], we call LINQ Select which applies an inner function that takes each byte and returns a zero-padded 2 digit Hex string. C# can do this nicely using the format specifier x2. The LINQ Select function returns an IEnumerable, so it’s easy to call ToArray() to get an array of string or string[].
    3. Now just call Join() method on an array of string and we end up with a concatenated string.

    Reference: https://dev.to/codeprototype/correctly-capture-ios-13-device-token-in-xamarin-1968

    Solution 2: This also works fine

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        byte[] result = new byte[deviceToken.Length];
        Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
        var token = BitConverter.ToString(result).Replace("-", "");
    }
    
    0 讨论(0)
  • 2020-12-03 05:25

    use deviceToken.debugDescription

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