8

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

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

谢谢

4

4 回答 4

10

没错,您必须将设备令牌从 转换NSDataNSString才能使用 JSON 对象发送它。但是您选择哪种转换方法完全取决于您或提供商的要求。最常见的方法是十六进制字符串(参见示例Best way to serialize an NSData into a hexadeximal string)或 Base64 字符串(使用 base64EncodedStringWithOptions)。两者都是100%“可靠”的。

此外,您应该始终将设备令牌发送给提供者,而不仅仅是在它发生更改时。提供者必须保留所有设备令牌的数据库以及最近一次发送的时间戳,以便将时间戳与“反馈服务”的可能响应进行比较。

于 2014-04-05T10:41:01.720 回答
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)];
}

完成上述代码行后,添加以下方法

#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);
}

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

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
于 2014-11-03T08:03:39.830 回答
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])];

将数据转换为字节意味着我们可以计算它。删除空格和 <> 真的不是一个好主意

于 2015-03-16T11:39:45.593 回答
0
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {

NSString *str = [NSString stringWithFormat:@"%@",_deviceToken];
//replace '<' and '>' along with spaces before you send it to the server.
}

这对我来说几乎在所有网络平台上都能可靠地工作。

于 2014-04-05T15:05:59.510 回答