在iOS 8.0及更高版本中不支持registerForRemoteNotificationTypes:,如何构建iOS应用程序?

18
如果设备注册通知的方式有重大变化,我们不能再使用`registerForRemoteNotificationTypes:`方法,那么如果不能使用Xcode 6 beta,我们该如何构建一个支持iOS 8的新版本应用程序呢?我们是否必须在Xcode 6 GM版本发布当天构建并提交应用程序,以便我们的用户继续接收推送通知?

为什么不能使用Xcode 6 beta? - mckeejm
1
我们可以使用 Xcode 6 beta 并提交到应用商店吗?我印象中苹果不会接受来自 Xcode beta 版本的构建。 - Jason Hocker
该方法不适用于iOS8,但仍可在iOS7上使用。使用Xcode 6 beta构建,然后在GA版本推出时编译和提交即可。您是正确的,在SDK版本退出beta版后才能提交iOS 8应用程序。现有用户将继续收到推送通知,因为他们的令牌不会失效,我相信。 - mckeejm
7个回答

50

iOS 8已更改通知注册方式。因此,您需要检查设备版本,然后注册通知设置。(请参考此链接。) 我在Xcode 6上尝试了这段代码,它对我有效。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        else
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
        }

     return YES;
}

嗨@Julldar,感谢您的评论。我已经编辑了我的内容。请修改您的建议。因为这段代码是有效的。 - Raşit ERASLAN
我从未说过它不起作用:)。实际上,我只是询问了一些澄清问题,你已经回答了,所以很好:)。我实际上会删除另一个评论:),并在一段时间后删除这个评论,以免使SO混乱。 - Patrice

17

你可能想考虑使用 respondsToSelector 而不是检查系统版本:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

11
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
#else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif

1
应该是 UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge - Raptor

3
根据苹果文档,iOS 8中registerForRemoteNotificationTypes:已被弃用,现在可以使用registerForRemoteNotificationsregisterUserNotificationSettings:
Xcode 6 beta和iOS 8 beta是预发布软件。Beta版本仅供开发和测试使用。新应用程序和应用程序更新必须使用发布版本的Xcode和iOS构建,才能提交到App Store。

3
if ([[[ UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
  [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

  [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
  [[UIApplication sharedApplication] registerForRemoteNotificationTypes(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

1
你可以使用这个,
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
        // for iOS 8
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [application registerForRemoteNotifications];
    }
    else
    {
        // for iOS < 8
        [application registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

    // RESET THE BADGE COUNT 
    application.applicationIconBadgeNumber = 0;

0

将以下代码写入AppDelegate didFinishLaunchingWithOptions函数中

 if([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) {
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
 }
        else {
UIRemoteNotificationType types = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
  }    [self.window makeKeyAndVisible];
return YES;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接