How to get device token in iOS 13 with Xamarin?

前端 未结 7 1861
悲&欢浪女
悲&欢浪女 2020-12-11 03:31

Our RegisteredForRemoteNotifications code broke because the token was retrieved with:

deviceToken.ToString().Trim(\'<\').Trim(\'>\').Replace(\" \", \"\         


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

    Here's another way to do:

    var bytes = deviceToken.ToArray<byte>();
    var hexStringArray = bytes.Select(b => b.ToString("x2")).ToArray();    
    var token = string.Join(string.Empty, hexStringArray);
    

    The code above is based on a post by NSHipster as I described in my own post https://medium.com/@kevinle/correctly-capture-ios-13-device-token-in-xamarin-3d0fa390b71b

    0 讨论(0)
  • 2020-12-11 03:58

    This worked for me on iOS 13. The linq option has better perf I'd think. Have to test iOS 12 to see if it's backwards compat. Worked on iOS 12 as well. This fixed my issue! Thanks for sharing with a code newb..

    0 讨论(0)
  • 2020-12-11 04:00

    Looks like I found the answer myself:

    byte[] result = new byte[deviceToken.Length];
    Marshal.Copy(deviceToken.Bytes, result, 0, (int) deviceToken.Length);
    var token = BitConverter.ToString(result).Replace("-", "");
    

    Using this code I was able to get a device token and send a notification.

    0 讨论(0)
  • 2020-12-11 04:02

    There is a field in deviceToken object called DebugDescription. Format that field and you will get the token.

    var myToken = deviceToken.DebugDescription.Replace("<", string.Empty).Replace(">", string.Empty).Replace(" ", string.Empty).ToUpper();
    
    0 讨论(0)
  • 2020-12-11 04:07

    Convert NSData to String

     var tokenStringBase64 =deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
    

    Convert String to NSData

    var deviceToken = new NSData(tokenStringBase64, NSDataBase64DecodingOptions.None);
    
    0 讨论(0)
  • 2020-12-11 04:11

    I´m having the same problem and was trying to implement based on this post: https://onesignal.com/blog/ios-13-introduces-4-breaking-changes-to-notifications/

    I´m going to test your solution the first chance I get but have you tested to see if it maintains compatibility with previous iOS builds?

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