2011-09-30 109 views
2

我能够在下面的方法中获取deviceToken,现在我想知道如何注册deviceToken进行推送通知,因为我不确定在获取设备标记后,使用哪种方法或API来注册设备标记推送通知以及此注册过程如何工作?哪种方法用于注册设备令牌以进行推送通知?

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSLog(@"APN device token: %@", deviceToken); 
} 
+1

没有办法,您需要将它发送到将发送通知的服务器。服务器将令牌存储在数据库中,并用它将通知发送到注册的设备。 –

回答

11

嗯,要开始我想确保如果您在应用程序启动时在registerForRemoteNotificationTypes中运行以下内容。这里是您可以添加到您的AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
       (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 

    self.window.rootViewController = self.tabBarController; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ 

    // Send the deviceToken to server right HERE!!! (the code for this is below) 

    NSLog(@"Inform the server of this device token: %@", deviceToken); 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ 
    // Place your code for what to do when the ios device receives notification 
    NSLog(@"The user info: %@", userInfo); 
} 


- (void)application:(UIApplication *) didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
    // Place your code for what to do when the registration fails 
    NSLog(@"Registration Error: %@", err); 
} 

当你提到注册推送通知的设备令牌您必须将deviceToken发送到您正在发送推送通知服务器并让服务器将它保存在数据库中为推。这里是一个如何将它发送到你的服务器的例子。

NSString *host = @"yourhost"; 
NSString *URLString = @"/register.php?id="; 
URLString = [URLString stringByAppendingString:id]; 
URLString = [URLString stringByAppendingString:@"&devicetoken="]; 

NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""]; 

URLString = [URLString stringByAppendingString:dt]; 
URLString = [URLString stringByAppendingString:@"&devicename="]; 
URLString = [URLString stringByAppendingString:[[UIDevice alloc] name]]; 

NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:URLString]; 
NSLog(@"FullURL=%@", url); 

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

如果您需要任何帮助,我将很乐意提供帮助。在任一网站上与我联系:Austin Web and Mobile GuruAustin Web Design

相关问题