axl*_*rtr 2 c# xamarin.ios ios xamarin xamarin.forms
我们的RegisteredForRemoteNotifications代码中断,因为使用以下方法检索了令牌:
deviceToken.ToString().Trim('<').Trim('>').Replace(" ", "");
Run Code Online (Sandbox Code Playgroud)
这曾经可以使用,但不适用于iOS 13,因为数据如下所示:
"{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }"
Run Code Online (Sandbox Code Playgroud)
对于如何使用目标c和swift正确执行此操作,有答案,但是我还没有找到使用C#的答案。
参考:
https://nshipster.com/apns-device-tokens/
如何用Xamarin做到这一点?
axl*_*rtr 17
看来我自己找到了答案:
byte[] result = new byte[deviceToken.Length];
Marshal.Copy(deviceToken.Bytes, result, 0, (int) deviceToken.Length);
var token = BitConverter.ToString(result).Replace("-", "");
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以获取设备令牌并发送通知。
这是另一种方法:
var bytes = deviceToken.ToArray<byte>();
var hexStringArray = bytes.Select(b => b.ToString("x2")).ToArray();
var token = string.Join(string.Empty, hexStringArray);
Run Code Online (Sandbox Code Playgroud)
上面的代码基于 NSHipster 的一篇文章,正如我在我自己的文章中所描述的https://medium.com/@kevinle/correctly-capture-ios-13-device-token-in-xamarin-3d0fa390b71b
归档时间: |
|
查看次数: |
1049 次 |
最近记录: |