【问题标题】:Get device token to push notify the second time on iPhone获取设备令牌以在 iPhone 上第二次推送通知
【发布时间】:2012-03-16 11:10:34
【问题描述】:

我需要在我的 iPhone 上获取设备令牌来测试推送通知。 在我的 iPhone 上,我已经同意通知推送权限。我尝试删除并重新安装该应用程序,但没有。我尝试在 didRegisterForRemoteNotificationsWithDeviceToken 方法中放置一个断点,但没有。

有什么建议吗?

这是我的代码:

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

【问题讨论】:

  • 您是否为启用推送通知配置了配置文件?
  • 是的,它已启用。我不明白,因为调试没有进入 didRegisterForRemoteNotificationsWithDeviceToken 方法。

标签: iphone objective-c push-notification apple-push-notifications devicetoken


【解决方案1】:

最好有另一个错误处理的委托方法:

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

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

  • 从 XCode 和设备中删除该应用的所有过期配置文件和所有配置文件
  • 检查是否为您的应用启用推送通知(在配置门户中)
  • 从门户下载配置文件,将其安装到 XCode
  • 检查所选配置文件(在构建设置/代码签名标识中)是否与应用匹配,而不是通用/通配符(有时自动选择出错)
  • 像往常一样(XCode 缓存“魔术”),最好在构建之前重新启动 XCode 并从设备中删除应用程序
  • 建设与祈祷

【讨论】:

  • 好的。我已插入此代码并返回错误。我更新了我的问题;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
相关资源
最近更新 更多