将设备令牌发送到服务器

Spi*_*ire 10 iphone token devicetoken

我已经阅读了很多这方面的教程,我只是想知道这是否是正确的方法

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

}
Run Code Online (Sandbox Code Playgroud)

任何建议都受到欢迎.

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

}
Run Code Online (Sandbox Code Playgroud)

我的应用程序适用于两种代码,但是正确的方法是什么?

a_a*_*eev 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
}
Run Code Online (Sandbox Code Playgroud)

要发送令牌,请创建新类(或使用现有类):
DataUpdater.h

#import <Foundation/Foundation.h>

@interface DataUpdater : NSObject     
+ (void)sendUserToken;
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

然后你可以调用[DataUpdater sendUserToken]; 在出现互联网连接的控制器中,或定期或在- (void)viewDidLoad- (void)viewWillAppear方法

我的建议:
1)我使用AFNetworking发送异步请求并检查服务器JSON响应
2)有时候最好使用Parse等服务来处理推送通知

  • 我想你应该调用[synchronize](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsuserdefaults_class/reference/reference.html#//apple_ref/occ/instm/NSUserDefaults/synchronize)存储信息后. (3认同)