Iphone – didRegisterForRemoteNotificationsWithDeviceToken: doesn’t invoke on calling registerForRemoteNotificationTypes:

apple-push-notificationsios-4.2iphonepush-notification

I am working with push notifications in a navBased app. in AppDelegate.m didRegisterForRemoteNotificationsWithDeviceToken: doesn't invoke on calling registerForRemoteNotificationTypes: code looks like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

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

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

    return YES;
}



- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Get a hex string from the device token with no spaces or < >
    NSLog(@"applicationDidFinishLaunchingWithOptions dev token test");

    NSString *deviceTokenStr = [[[[deviceToken description]
                          stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                         stringByReplacingOccurrencesOfString: @">" withString: @""] 
                        stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"Device Token: %@", deviceTokenStr);
}

I am quite sure that provisioning profile is not the problem. and i found error:

Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application" UserInfo=0x115490 {NSLocalizedDescription=no valid 'aps-environment' entitlement string found for application}

can anyone tell me whats going on in this code and y its not working? thanx

Best Answer

Enter this code in Appdelegate didFinishLaunching method this -

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

and then just also copy and paste these two methods in same appdelegate -

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
        NSString *dToken = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    dToken = [dToken stringByReplacingOccurrencesOfString:@" " withString:@""];


    NSLog(@"STR%@",dToken);


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{

    NSLog(@"Failed to get token, error: %@", error);
}
Related Topic