iOS APNS:以字符串格式将设备令牌发送给提供者

App*_*Dev 8 string-formatting apple-push-notifications devicetoken ios

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

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

谢谢

Mar*_*n R 10

你是正确的,你必须给设备令牌转换从NSDataNSString到能与JSON对象发送.但您选择的转换方法完全取决于您或提供商的要求.最常用的方法是十六进制字符串(请参阅例如将NSData序列化为十六进制字符串的最佳方法)或Base64字符串(使用 base64EncodedStringWithOptions).两者都是100%"可靠".

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


Rah*_*jan 8

在didFinishLaunchingWithOptions方法中

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
Run Code Online (Sandbox Code Playgroud)

执行上面的代码行后,添加以下方法

#pragma mark Push Notifications
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString  *token_string = [[[[deviceToken description]    stringByReplacingOccurrencesOfString:@"<"withString:@""]
                        stringByReplacingOccurrencesOfString:@">" withString:@""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];
NSString* strURL = [NSString stringWithFormat:@"http://www.sample.com?device_token=%@&type=IOS",token_string];
strURL=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",strURL);
NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSLog(@"content---%@", fileData);
}
Run Code Online (Sandbox Code Playgroud)

在上面列出的步骤之后,您可以使用此委托功能来检索并处理推送通知.下面添加的方法将触发应用程序在后台运行或不运行.下面给出的方法可从ios7.0获得

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Run Code Online (Sandbox Code Playgroud)


Gau*_*ain 6

const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                      ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                      ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                      ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
Run Code Online (Sandbox Code Playgroud)

将数据转换为字节意味着我们可以对其进行计数.删除空格和<>确实不是一个好主意