简体   繁体   中英

Allowing Push Notifications Version Control

Swift 3 IOS 10: I've been looking for code for allowing pushing notification; finally, I could find a precisely useful one from www.codementor.io . However, I came into puzzle. I wonder if the following code will work with the lower or newer version of iOS. Since Apple will be releasing its version relentlessly, how the following code will be able to handle the changes? Is there any way out to deal with the problem I mentioned?

// iOS 10 support

if #available(iOS 10, *) {

UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }

application.registerForRemoteNotifications()

}

// iOS 9 support

else if #available(iOS 9, *) {

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))

UIApplication.shared.registerForRemoteNotifications()

}

// iOS 8 support

else if #available(iOS 8, *) {

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))

UIApplication.shared.registerForRemoteNotifications()

}

// iOS 7 support

else {

application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])

}

Try this:

func setUpPushNotification(application: UIApplication) {
        if #available(iOS 10.0, *) {
        let notificationTypes: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: notificationTypes,
            completionHandler: {_,_ in })
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
        }
        else{
            let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound]
            let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
            application.registerUserNotificationSettings(pushNotificationSettings)

        }
           application.registerForRemoteNotifications()
    }

Yes the code for iOS 10 will work for iOS 11 as well.

Basically the #available macro checks the minimum OS so it's also safe to merge iOS 8 and 9.

//iOS 10+
if #available(iOS 10, *) {
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
        if (granted) { //check if authorization was granted before registering
            application.registerForRemoteNotifications()
        }
    }
}

// iOS 8+ support
else if #available(iOS 8, *) {
    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    UIApplication.shared.registerForRemoteNotifications()
}

// iOS 7 support
else {
    application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
if #available(iOS 10, *)

reads as: if iOS from Version 10 or above is available...

So if you leave your code like that and iOS 11, 12, etc is coming along they will all go into this if branch.

So your code will work as long as the Push API is not changed. If Apple changes the API in the future (say in iOS 12) you will have to add another if-branch and re-submit you app to the store...

(If someone with iOS 9 uses your app they will end up in the second if branch - because the first one does not apply. So the order of the if s guarantees that lower iOS version get the right code.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM