0

我正在尝试为对讲机推送通知注册应用程序。他们在这里有说明:https ://docs.intercom.io/Install-on-your-mobile-product/enabling-push-notifications-with-intercom-for-ios

他们在 obj c 中给出了这段代码,但我的应用程序很快,这对我来说就像胡言乱语:

- (void)applicationDidBecomeActive:(UIApplication *)application {    
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ // iOS 8 (User notifications)
        [application registerUserNotificationSettings:
         [UIUserNotificationSettings settingsForTypes:
          (UIUserNotificationTypeBadge |
           UIUserNotificationTypeSound |
           UIUserNotificationTypeAlert)
                                           categories:nil]];
        [application registerForRemoteNotifications];
    } else { // iOS 7 (Remote notifications)
        [application registerForRemoteNotificationTypes:
         (UIRemoteNotificationType)
         (UIRemoteNotificationTypeBadge |
          UIRemoteNotificationTypeSound |
          UIRemoteNotificationTypeAlert)];
    }
}

有人可以解释如何快速完成这个过程吗?

4

1 回答 1

1

以下代码通过使用添加到 Swift 2 的新可用性功能在 Xcode 7.1 上运行

if #available(iOS 8, *) {
    application.registerUserNotificationSettings(
            UIUserNotificationSettings(forTypes: UIUserNotificationType(rawValue: UIUserNotificationType.Badge.rawValue |
                UIUserNotificationType.Sound.rawValue |
                UIUserNotificationType.Alert.rawValue),
            categories: nil))
} else {
    application.registerForRemoteNotificationTypes(
        UIRemoteNotificationType(rawValue: UIRemoteNotificationType.Badge.rawValue |
        UIRemoteNotificationType.Alert.rawValue |
        UIRemoteNotificationType.Sound.rawValue))
}
于 2015-10-28T18:15:20.857 回答