which method is used to register the device token for push notification?
I am able to get the deviceToken i开发者_高级运维n the below method, now I want to know how to register the deviceToken for push notification,because I am not sure after getting the device token which method or API is used to register the device token for Push Notification and how this registration process works?
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"APN device token: %@", deviceToken);
}
Well, to start I want to make sure that if you are running the following in the registerForRemoteNotificationTypes
when the app launches. Here is what you can add to your 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);
}
When you mention registering the device token for push notification you have to send the deviceToken to you server that is sending the push notifications and have the server save it in the database for the push. Here is an example of how you can send this to your server.
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:@"&amp;amp;amp;amp;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];
if you need anymore help I will be happy to help. Contact me on either website: Austin Web and Mobile Guru or Austin Web Design
精彩评论