如何使用 Xamarin 在 iOS 13 中获取设备令牌? [英] How to get device token in iOS 13 with Xamarin?

查看:26
本文介绍了如何使用 Xamarin 在 iOS 13 中获取设备令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 RegisteredForRemoteNotifications 代码损坏,因为令牌是通过以下方式检索的:

Our RegisteredForRemoteNotifications code broke because the token was retrieved with:

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

这曾经适用,但不适用于 iOS 13,因为数据将如下所示:

This used to work but not with iOS 13 because the data will be like this:

"{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }"

有关于如何使用目标 c 和 swift 正确执行此操作的答案,但我还没有找到使用 C# 的答案.

There's answers for how to do this correctly with objective c and swift but I haven't found one with C#.

参考:

获取推送通知的设备令牌

https://nshipster.com/apns-device-tokens/

如何使用 Xamarin 执行此操作?

How to do this with Xamarin?

推荐答案

看来我自己找到了答案:

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.

这篇关于如何使用 Xamarin 在 iOS 13 中获取设备令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆