Enable APNS in iOS Code for Objective C Developer

Hello Friend ,here is the code for APNS , you need to use mobile-provisioning file with APNS enable AppID.
and you need to pass device token to backend side and webservice will automatically push notification to the device , which device register their device token to that werbservice.

All code you need to put into appdelegate.m file :

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

put this code in didfinishlaunching method of your AppDelegate.m file.

2 ) now you need to copy and paste below three function to your AppDelegate.m file , and your work is over ūüôā

– (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
   tokenAsString = [[[[devToken description]
¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@””]]
¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†stringByReplacingOccurrencesOfString:@” ” withString:@””] copy];
   deviceToken = [[NSString stringWithString:tokenAsString] retain];
¬† ¬†NSLog(@”\n DeviceToken :%@”,deviceToken);
[18/07/12 1:40:17 PM] deval chauhan: }

this function gives you device token in nsstring format , you need to pass this to backend side using webservice.

– (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
¬† ¬†UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Error in didFailToRegisterForRemoteNotificationsWithError !” message:[NSString stringWithFormat:@”%@”,error] delegate:self cancelButtonTitle:@”ok” otherButtonTitles:nil];
   [alert show];
   [alert release];
}
– (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
   
¬† ¬†UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”VG 2013″ message:[NSString stringWithFormat:@”%@”,[[userInfo valueForKey:@”aps”] valueForKey:@”alert”]] delegate:self cancelButtonTitle:@”ok” otherButtonTitles:nil];
   [alert show];
   [alert release];
}

this both function handles your notification massages and errors automatically.

you need to take care about mobile-provisioning files.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s