Unable to get deviceToken

Discussion in 'iPhone/iPad Programming' started by rahulvyas, Nov 30, 2009.

  1. macrumors newbie

    Joined:
    May 6, 2009
    #1
    I want to use push notification in my app.I have created an unique app id and created push development certificate.also created provisioning profile using that unique app id.I put the code for registering the device in application terminate method so that when my app closed it's enabled push notification and when it launches it disables the push notification.BTW currently i am using ApplicationDidFnishLaunching method and registering for push notification.The app is registered for push notification i think because it asks to allow push notification on alertview.But i've never found device token.The app is not invoking the method
    Code:

    Code:
    - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    	NSLog(@"devToken=%@",deviceToken);
    	[self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",deviceToken] cancleButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitle:@""];
    }
    even i set a breakpoint in this method but it's not invoking.
    i have iPhone 3GS jailbroken. Anybody knows why i'm not getting devicetoken.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What class is that method in? If it's not your application delegate it's not going to work...
     
  3. thread starter macrumors newbie

    Joined:
    May 6, 2009
    #3
    it's in appdelegate
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
  5. thread starter macrumors newbie

    Joined:
    May 6, 2009
    #5
    here is what i have put in appdelegate
    Code:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {	
        // Override point for customization after application launch
    	BOOL result=application.enabledRemoteNotificationTypes;
    	if(result)
    	{
    		[[UIApplication sharedApplication] unregisterForRemoteNotifications];
    	}
    	else
    	{
    		//Register for notifications
    		[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
    		[self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitle:@""];
    		
    	}
    	[window setBackgroundColor:[UIColor colorWithRed:96/256.0 green:143/256.0 blue:191/256.0 alpha:.8]];
    	[window addSubview:[viewController view]];
        [window makeKeyAndVisible];
    	
    }
    
    // Delegation methods 
    - (void)application:(UIApplication *)app 
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken { 
        const void *devTokenBytes = [devToken bytes];
    	NSLog(@"devToken=%@",devTokenBytes);
    	[self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",devTokenBytes] cancleButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitle:@""];
    	
    	
    }
    
    - (void)application:(UIApplication *)app 
    didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
        NSLog(@"Error in registration. Error: %@", err); 
    	[self alertNotice:@"" withMSG:[NSString stringWithFormat:@"Error in registration. Error: %@", err] cancleButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitle:@""];
    
    }
    	 
    -(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle{
    	 UIAlertView *alert;
    	 if([otherTitle isEqualToString:@""])	 
    		 alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];
    	 else
    		 alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle,nil];
    	 [alert show];
    	 [alert release];
    }
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Can you post an entire console log of a run (so we can see all the NSLog output)?
     
  7. thread starter macrumors newbie

    Joined:
    May 6, 2009
    #7
    Log shows nothing but i get this alert when my view loads

    Code:
    [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitle:@""];
    and settings for push notification appears in settings app.

    and when i unregistered settings removed from settings app.

    also tell me if this possible "enable notification when app terminates and disable when app launches".
     

Share This Page