Im using the push notification service in my app.
But it is now always showing(when app launches) that it is failed to register for pushnotification.
i used the below code for push notifications:
Can anyone help me where i’m going wrong.
Thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(application.applicationIconBadgeNumber != 0){
[[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:@"new"];}
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"asdfasdf");
NSString *str = [NSString
stringWithFormat:@"Device Token=%@",deviceToken];
const char* data = [deviceToken bytes];
NSMutableString* token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://chargrilled.k-hosting.co.uk/test2/register_device.php?dt=%@",token]];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]autorelease];
NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:self];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Device registered for push notification."
message:nil
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Okay", nil];
[alert show];
[alert release];
}
- (NSString*)stringWithDeviceToken:(NSData*)deviceToken {
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Device registration failed."
message:nil
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Okay", nil];
[alert show];
[alert release];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
int i;
for (id key in userInfo) {
i++;
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
if(i==2){
}
}
application.applicationIconBadgeNumber = [[userInfo objectForKey:@"badge"] integerValue];
[[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:@"new"];
}
Check the profile u are using, is it enable for push notification. If not create a new with push notification.