PDA

View Full Version : Unable to get deviceToken




rahulvyas
Nov 30, 2009, 03:25 AM
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:

- (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.



robbieduncan
Nov 30, 2009, 04:02 AM
What class is that method in? If it's not your application delegate it's not going to work...

rahulvyas
Nov 30, 2009, 04:27 AM
What class is that method in? If it's not your application delegate it's not going to work...

it's in appdelegate

robbieduncan
Nov 30, 2009, 04:32 AM
The documentation is not giving much away. It suggests this will be called automatically after registerForRemoteNotificationTypes: (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/registerForRemoteNotificationTypes:) is successful. Perhaps you are getting an error in registration? Do you implement application:didFailToRegisterForRemoteNotificationsWithError: (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didFailToRegisterForRemoteNotificationsWithError:)? If not try implementing that to see if there is an error with the registration...

rahulvyas
Nov 30, 2009, 05:47 AM
here is what i have put in appdelegate

- (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];
}

robbieduncan
Nov 30, 2009, 05:50 AM
Can you post an entire console log of a run (so we can see all the NSLog output)?

rahulvyas
Nov 30, 2009, 06:55 AM
Log shows nothing but i get this alert when my view loads

[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".