哪个方法用于注册设备令牌以进行推送通知?

Use*_*321 2 iphone registration push-notification devicetoken ios

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

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"APN device token: %@", deviceToken);
}
Run Code Online (Sandbox Code Playgroud)

小智 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);
}
Run Code Online (Sandbox Code Playgroud)

当您提到为推送通知注册设备令牌时,您必须将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];
Run Code Online (Sandbox Code Playgroud)

如果您需要更多帮助,我将很乐意为您提供帮助.请在任一网站上与我联系:Austin Web和Mobile GuruAustin Web Design