iOS APNS:以字符串格式将设备令牌发送给提供者 [英] iOS APNS: sending the device token to the provider in string format

查看:155
本文介绍了iOS APNS:以字符串格式将设备令牌发送给提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过调用我的请求中需要JSON数据的服务,将我的iOS应用程序的APNS设备令牌发送给我的提供商。我正在阅读Apple的本地和推送通知编程指南,它只说应用程序:didRegisterForRemoteNotificationsWithDeviceToken:委托方法将设备令牌传递为 NSData ,您应将其传递给以二进制数据编码的提供程序。但是我需要将它转换为字符串才能向我的提供者发送JSON请求。

I need to send the APNS device token of my iOS app to my provider by calling a service that expects JSON data in my request. I'm reading Apple's Local and Push Notification Programming Guide and it only says that the application:didRegisterForRemoteNotificationsWithDeviceToken: delegate method passes the device token as NSData and you should pass it to your provider encoded in binary data. But I need it to be converted to string in order to be able to send a JSON request to my provider.

我也阅读了几篇与此相关的帖子,因为它看起来是常见的情况,但我发现了一些不同的方法将这种设备令牌转换为字符串来发送它,我不确定它们中哪一个应该是最合适的。处理这个问题最可靠的方法是哪种?我想我的提供者需要将此字符串转换回调用APNS,我还需要在应用程序中存储此令牌,以便在生成新令牌并且应用程序时安全地将其与新值进行比较:didRegisterForRemoteNotificationsWithDeviceToken:被调用,仅在它被更改时发送令牌。

I've also been reading several posts related to this, since it looks it is a common scenario, but I've found some different ways to convert such device token to string to send it, and I'm not sure which of them should be the most appropriate. Which would the most reliable way to deal with this be? I suppose my provider will need to convert this string back to call APNS, and I also need to store this token in the app in order to safely compare it with the new value if a new token is generated and application:didRegisterForRemoteNotificationsWithDeviceToken: is called, to send the token only if it has changed.

谢谢

推荐答案

你必须将设备令牌从 NSData 转换为 NSString
能够使用JSON对象发送它。但是您选择的转换方法完全取决于您的b $ b或提供商的要求。最常见的方法是
十六进制字符串(参见例如将NSData序列化为十六进制字符串的最佳方法)或Base64字符串(使用
base64EncodedStringWithOptions )。两者都是100%可靠。

You are right that you have to convert the device token from NSData to NSString to be able to send it with a JSON object. But what conversion method you choose is completely up to you or the requirements of the provider. The most common methods are a hex string (see for example Best way to serialize an NSData into a hexadeximal string) or a Base64 string (using base64EncodedStringWithOptions). Both are 100% "reliable".

此外,您应始终将设备令牌发送给提供商,而不仅仅是在更改时。提供商必须在最近一次发送时保留所有设备令牌的数据库,其时间戳为
,以便将时间戳与反馈服务中可能的响应
进行比较。

Also you should always send the device token to the provider and not only when it has changed. The provider has to keep a database of all device tokens with a timestamp of when it was sent last recently, in order to compare the timestamp with a possible response from the "feedback service".

这篇关于iOS APNS:以字符串格式将设备令牌发送给提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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