2011-09-20 98 views
10

我读了很多的教程,这和我只是想TI知道这是做这个发送设备令牌服务器

- (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

方法您的代码不能正常工作? – Nekto

+1

看起来不错,但是,我会1,添加错误恢复机制(如果请求失败,您将失去您的令牌!)和b,使用异步请求。 – mja

+1

异步?你是什​​么意思。请给我一些错误恢复机制的指导方针。该代码的工作原理,但我想知道这是否正确的方式,因为我读过的地方'NSString * newToken = [deviceToken描述];'这是不正确的方式获取数据到字符串 – Spire

回答

18

由于MJA在评论中说,这是更好地添加错误恢复机制(如果请求失败,你会失去你的令牌),并使用异步请求(在后台发送令牌)

在你AppDelegate.m:

​​

要发送的令牌创建新类(或者利用现有的一个):
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 

然后就可以调用[DataUpdater sendUserToken]当Internet连接出现在控制器,或定期或在- (无效)viewDidLoad中- (无效)viewWillAppear中方法

我的建议:
1)我用AFNetworking发送异步请求,并检查服务器JSON响应
2)有些时候,它更好地使用服务,如Parse与推送通知工作

+0

Thx哥们,但这是一个非常非常古老的问题。虐待接受答案,这可能会使某人受益。 – Spire

+3

我想你应该调用[同步](https://developer.apple。com/library/mac/documentation/cocoa/reference/foundation/classes/nsuserdefaults_class/reference/reference.html#// apple_ref/occ/instm/NSUserDefaults/synchronize)。 – nsacerdote

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); 
} 
+0

didFinishLaunchingWithoptions中的代码必须添加才能获取推送通知 –

相关问题