注册不能在Xcode 6中工作的远程通知

时间:2014-10-31 04:12:24

标签: ios objective-c xcode apple-push-notifications itunesconnect

我在Xcode 6中编写了以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }

    return YES;
}

我在以下两种方法中设定了断点:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //Registration successful
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    //Registration failed
}

但是当我在运行iOS 8的设备上测试时,这两种方法永远不会被触发。我也尝试使用iOS 7,仍然没有成功。所以我认为我的配置文件可能存在问题。因此,为了确认,我打开了开发者帐户,在App ID中我确认启用了APNS,撤销了现有证书,为推送通知和开发创建了新的开发证书。从密钥链中删除旧证书。清除Xcode中的所有配置文件,将新创建的证书添加到钥匙串中,使用新创建的证书创建开发配置文件,将其添加到Xcode并使用新创建的配置文件再次在两个设备上运行我的应用程序。仍然没有结果。然后我在Xcode 5中打开了项目,更改了didFinishLaunching方法,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];

    return YES;
}

使用我刚刚创建的相同配置文件在两台设备上运行应用程序。它运行得很好,- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken中的断点受到了打击。在使用Xcode 5进行编译时,使用UIRemoteNotificationType在iOS 7和iOS 8上进行远程通知注册成功!!!

所以我归档并将申请提交给iTune connect的试飞。我第一次从iTunes Connect收到这封邮件:

  

缺少推送通知权利 - 您的应用似乎包含   API用于注册Apple推送通知服务,但是   应用签名的权利不包括" aps-environment"   权利。如果您的应用使用Apple推送通知服务,   确保您的App ID已启用推送通知   配置门户,并在使用a签名您的应用程序后重新提交   分发配置文件,包括" aps-environment"   权利。请参阅"供应和开发"在本地和推送   通知编程指南了解更多信息。如果你的应用程序   不使用Apple推送通知服务,不需要采取任何措施。   您可以从将来的提交中删除API以停止此警告。   如果您使用第三方框架,则可能需要联系   开发人员获取有关删除API的信息。

所以我重新创建了生产APNS证书,生产证书,删除旧证书后将它们添加到钥匙串,使用新创建的生产证书为App Store创建分发配置文件,将其添加到Xcode,归档并再次提交给测试航班。这次邮件没有来,但是远程通知代理方法都没有触发(我已经放置了一个AlertView来知道何时触发了这两种方法)。

有人能告诉我这种奇怪行为的原因吗?我在这做错了什么?我该如何纠正它?

1 个答案:

答案 0 :(得分:2)

确定。我终于找到了解决方案。对于IOS 8,我必须添加以下行:

[[UIApplication sharedApplication] registerForRemoteNotifications];

因此didFinishLaunchingWithOptions的代码如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }

    return YES;
}

奇怪的是我在其他情况下没有改变任何东西,但现在它适用于IOS 7和IOS 8.希望它对某人有帮助。我花了两天多的时间在它上面,当我在发布问题后立即找到解决方案时,我讨厌它。