1

我需要获取我的iPhone上的设备令牌来测试推送通知。 在我的iPhone上,我已经同意通知推送权限。我尝试删除并重新安装应用程序,但没有。我尝试在didRegisterForRemoteNotificationsWithDeviceToken方法中加入一个缺点,但没有任何结果。获取设备令牌推送通知第二次在iPhone上

有什么建议吗?

这是我的代码:

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

这是错误,它打印:

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} 
+0

你的推送通知设置启用配置文件? – 2012-03-16 11:24:30

+0

是的,它启用。我不明白,因为调试不输入在didRegisterForRemoteNotificationsWithDeviceToken方法。 – Dany 2012-03-16 11:26:40

回答

4

这是好事,有另一个用于错误处理的委托方法:

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

问题更新后,更清楚的是问题出在错误的配置文件中(通用或没有'aps-evironment') 。所以:

  • 删除所有过期的配置文件和所有配置从两个Xcode和设备的应用程序
  • 检查推送通知是为您的应用程序(在配置门户)从门户
  • 下载置备配置文件中启用,安装Xcode的
  • 检查所选配置文件(在构建设置/代码签名身份)的应用相匹配,而不是通用/通配符(有时也autoselection出错)
  • 像往常一样(的XCode缓存“神奇”),这是更好重新启动XCode并删除应用程序从设备之前建立
  • 构建&祈祷
+0

好的。我插入了这段代码,它返回一个错误。我更新我的问题;) – Dany 2012-03-16 16:40:49

+0

我更新我的答案) – 2012-03-18 21:13:27