Pergunta

I need to obtain the device token on my iPhone to test the push notify. On my iPhone I had already agreed to notify push permissions. I try to remove and reinstall the app but nothing. I try to put a breackpoint in the didRegisterForRemoteNotificationsWithDeviceToken method but nothing.

Any suggestion?

This is my code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    /**** PUSH NOTIFY ****/
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}


- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"http://www.mysite.com/storeToken.php?task=register&token=%@", [self stringWithDeviceToken:deviceToken]];
    //NSLog(@"%@",str);
    NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];    
    [pref setObject:[self stringWithDeviceToken:deviceToken] forKey:@"token"];
    [pref synchronize];

    NSURL *url = [NSURL URLWithString:str];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];

    [str release];
}

- (NSString*)stringWithDeviceToken:(NSData*)deviceToken {
    const char* data = [deviceToken bytes];
    NSMutableString* token = [NSMutableString string];

    for (int i = 0; i < [deviceToken length]; i++) {
        [token appendFormat:@"%02.2hhX", data[i]];
    }

    return [[token copy] autorelease];
}

This is the error that it print:

Error: Error Domain=NSCocoaErrorDomain Code=3000 "nessuna stringa di autorizzazione 'aps-environment' valida trovata per l'applicazione" UserInfo=0x296e80 {NSLocalizedDescription=nessuna stringa di autorizzazione 'aps-environment' valida trovata per l'applicazione}
Foi útil?

Solução

It is good to have another delegate method for error handling:

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Fail to register for remote notifications: %@", [error localizedDescription]);
}

After question update it's more clear that problem is in wrong provisioning profile (generic or without 'aps-evironment'). So:

  • Remove all expired profiles and all profiles for that app from both XCode and device
  • Check push notifications are enabled for your app (in provisioning portal)
  • Download provisioning profile from portal, install it to XCode
  • Check that selected profile (in build settings / codesigning identity) matches the app, and is not the generic/wildcard one (sometimes autoselection goes wrong)
  • As usual (XCode caching "magic"), it's better to restart XCode and remove the application from device before build
  • Build & Pray
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top