簡體   English   中英

哪個方法用於注冊設備令牌以進行推送通知?

[英]which method is used to register the device token for push notification?

我能夠在下面的方法中獲取deviceToken,現在我想知道如何為push通知注冊deviceToken,因為我不確定在獲取設備令牌之后使用哪個方法或API來注冊推送通知的設備令牌以及此注冊過程如何運作?

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

好吧,首先,我想確保在應用程序啟動時在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和Mobile GuruAustin Web Design

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM