2011-09-30 107 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

相關問題