注册 Apple 推送通知 - 如何通过 HTTP 传递 DevToken?
在 iPhone 推送通知文档中,他们有一个代码片段,其中重写了接收设备令牌的 UIApplication 方法 -
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
我的问题是,当我实现自己的自定义方法时,如何使用 devTokenBytes?
我需要使用 NSURLConnection (我想)创建一个 HTTP 请求,它将令牌传递给我的服务器端提供商应用程序。 我明白了那部分,但我不确定如何将 devTokenBytes 添加到请求中? 我的第一直觉是使用字节来创建 String 对象,但是当我尝试使用 NSASCIIStringEncoding 时,我得到了奇怪的混乱字符。 我看到 NSData 的“bytes”方法的返回类型是一个指针,但我不知道如何处理它。 将此令牌放入请求中的正确方法是什么?
该文档还详细介绍了“应用程序应该与其提供者连接并向其传递这个以二进制格式编码的令牌。” 但我不知道如何处理以这种方式编码的东西。
In the iPhone Push Notification documentation, they have a code snippet in which they override the UIApplication method that receives a device token -
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
My question is, when I implement my own custom method, how do I use the devTokenBytes?
I need to create an HTTP Request, using NSURLConnection (I suppose), that will hand off the token to my server-side provider app. I get that part, but I'm not sure how to add devTokenBytes to the request? My first instinct was to use the bytes to create a String object, but when I try to using NSASCIIStringEncoding I get a weird jumbled mess of characters. I see that the return type of NSData's "bytes" method is a pointer, but I don't know what to do with it. What's the correct way to put this token into a request?
The documentation also details - "he application should connect with its provider and pass it this token, encoded in binary format." But I don't know how to handle something encoded in this manner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎处理这个问题的最简单方法是使用 NSData 的“description”方法的返回值。 它将返回一个字符串,您可以使用它来获得 64 个字符的表示形式。
Seems like the easiest way to handle this is to use the return value from the "description" method of NSData. It'll return a String that you can play with a bit to get a 64 character representation.
我不确定这是否是您在寻找的:
Im not sure if this is you looking for:
我还没有尝试过,但我的第一个猜测是查看 Base64 将二进制数组编码为 7 位干净字符串,该字符串可以作为请求的查询参数传递。
您也可以将数据发布到 URL,但我认为对其进行编码会更容易。
I haven't tried it out yet, but my first guess would be to look at Base64 encoding the binary array into a 7-bit clean string that can be passed as a query parameter on your request.
You could also POST the data to a URL instead, but I would think encoding it would be easier.