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

后端 未结 7 1744
自闭症患者
自闭症患者 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条回答
  •  -上瘾入骨i
    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();
        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("-", "");
    }
    

提交回复
热议问题