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

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

问题描述

我需要通过调用在我的请求中需要 JSON 数据的服务,将我的 iOS 应用程序的 APNS 设备令牌发送给我的提供者.我正在阅读 Apple 的 本地和推送通知编程指南 并且它只说application: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,并且我还需要将此令牌存储在应用程序中,以便在生成新令牌和 application: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 对象发送它.但是你选择的转换方式完全是取决于您或提供商的要求.最常见的方法是十六进制字符串(参见例如 序列化 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天全站免登陆