iosswiftintercomintercom-ios

Intercom push notifications in swift


I am trying to register an app for intercoms push notifications. They have instructions here: https://docs.intercom.io/Install-on-your-mobile-product/enabling-push-notifications-with-intercom-for-ios

They give this code in obj c, but my app is in swift and this looks like gibberish to me:

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

Could someone explain how to do this process in swift?


Solution

  • Following code works on Xcode 7.1 by using the new availability feature added to Swift 2

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