简体   繁体   English

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

[英]Registering for Apple Push notification - how do I pass the DevToken with HTTP?

In the iPhone Push Notification documentation, they have a code snippet in which they override the UIApplication method that receives a device token - 在iPhone推送通知文档中,它们具有一个代码段,其中它们覆盖了接收设备令牌的UIApplication方法-

- (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? 我的问题是,当实现自己的自定义方法时,如何使用devTokenBytes?

I need to create an HTTP Request, using NSURLConnection (I suppose), that will hand off the token to my server-side provider app. 我需要使用NSURLConnection(我想)创建一个HTTP请求,该请求会将令牌传递给我的服务器端提供程序应用程序。 I get that part, but I'm not sure how to add devTokenBytes to the request? 我得到了那部分,但是我不确定如何将devTokenBytes添加到请求中? 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. 我的第一个直觉是使用字节创建String对象,但是当我尝试使用NSASCIIStringEncoding时,我得到了一个奇怪的混乱字符。 I see that the return type of NSData's "bytes" method is a pointer, but I don't know what to do with it. 我看到NSData的“字节”方法的返回类型是一个指针,但是我不知道该怎么做。 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. 但是我不知道如何处理以这种方式编码的东西。

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. 我还没有尝试过,但是我的第一个猜测是看一下Base64将二进制数组编码成7位干净的字符串,该字符串可以作为请求中的查询参数传递。

You could also POST the data to a URL instead, but I would think encoding it would be easier. 您也可以改为将数据发布到URL,但我认为对它进行编码会更容易。

Seems like the easiest way to handle this is to use the return value from the "description" method of NSData. 似乎最简单的处理方法是使用NSData的“描述”方法中的返回值。 It'll return a String that you can play with a bit to get a 64 character representation. 它将返回一个String,您可以将其稍作播放以获取64个字符的表示形式。

Im not sure if this is you looking for: 我不确定这是否是您在寻找:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM