我已经阅读了很多这方面的教程,我只是想知道这是否是正确的方法
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *urlString = [NSString stringWithFormat:@"http://myhost.com./filecreate.php?token=%@",newToken];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
}
任何建议都受到欢迎。
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
const char* data = [deviceToken bytes];
NSMutableString* token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}
NSString *urlString = [NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",token];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
}
我的应用程序适用于两种代码,但是正确的方法是什么?
答案 0 :(得分:18)
正如 mja 在评论中所说,最好添加错误恢复机制(如果请求失败,您将丢失令牌)并使用异步请求(在后台发送令牌)
在你的AppDelegate.m中:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString * token = [NSString stringWithFormat:@"%@", deviceToken];
//Format token as you need:
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"apnsToken"]; //save token to resend it if request fails
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"apnsTokenSentSuccessfully"]; // set flag for request status
[DataUpdater sendUserToken]; //send token
}
发送令牌创建新类(或使用现有类):
DataUpdater.h
#import <Foundation/Foundation.h>
@interface DataUpdater : NSObject
+ (void)sendUserToken;
@end
DataUpdater.m
#import "DataUpdater.h"
@implementation DataUpdater
+ (void)sendUserToken {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"apnsTokenSentSuccessfully"]) {
NSLog(@"apnsTokenSentSuccessfully already");
return;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"apnsToken"]]]; //set here your URL
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error == nil) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"apnsTokenSentSuccessfully"];
NSLog(@"Token is being sent successfully");
//you can check server response here if you need
}
}];
}
@end
然后,当出现互联网连接时,或者定期或在 - (void)viewDidLoad 或 - 中,您可以在控制器中调用 [DataUpdater sendUserToken]; void)viewWillAppear 方法
我的建议:
1)我使用AFNetworking发送异步请求并检查服务器JSON响应
2)有时候使用诸如Parse之类的服务来处理推送通知
答案 1 :(得分:0)
在didFinishLaunchingWithOptions方法
中[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
完成上述代码行后,添加以下方法
#pragma mark -
#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);
}