注册 Apple 推送通知 - 如何通过 HTTP 传递 DevToken?

发布于 2024-08-01 14:54:44 字数 713 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╄→承喏 2024-08-08 14:54:44

似乎处理这个问题的最简单方法是使用 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.

横笛休吹塞上声 2024-08-08 14:54:44

我不确定这是否是您在寻找的:

deviceTokenString = [[[[[deviceToken description]
                            stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                           stringByReplacingOccurrencesOfString: @">" withString: @""] 
                          stringByReplacingOccurrencesOfString: @" " withString: @""] retain];

Im not sure if this is you looking for:

deviceTokenString = [[[[[deviceToken description]
                            stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                           stringByReplacingOccurrencesOfString: @">" withString: @""] 
                          stringByReplacingOccurrencesOfString: @" " withString: @""] retain];
2024-08-08 14:54:44

我还没有尝试过,但我的第一个猜测是查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文