【问题标题】:Allowing Push Notifications Version Control允许推送通知版本控制
【发布时间】:2017-08-23 07:01:37
【问题描述】:

Swift 3 IOS 10:我一直在寻找允许推送通知的代码;最后,我可以从www.codementor.io 找到一个非常有用的。然而,我陷入了困惑。我想知道以下代码是否适用于较低或较新版本的 iOS。既然 Apple 将无情地发布它的版本,那么下面的代码将如何处理这些变化?有没有办法解决我提到的问题?

// 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])

}

【问题讨论】:

  • 您的代码是正确的。只需将 iOS 9、8 和 7 组合成一个块即可。 iOS 10 在单独的块中。
  • 如果我这样做了,如果新发布的iOS版本开始使用,这段代码还会生效吗?谢谢你的回答。
  • 您正在检查#available 标记,这将验证设备的操作系统版本并执行该块。从 iOS 10 到现在(iOS 11),仍然使用 UNUserNotification 类。 Apple 更改此框架后,您必须像现在为 iOS 10 所做的那样支持新框架。
  • @Basheer,非常感谢!!

标签: ios swift version-control push-notification notifications


【解决方案1】:

是的,iOS 10 的代码也适用于 iOS 11。

基本上,#available 宏会检查最低操作系统,因此合并 iOS 8 和 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])
}

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:
      if #available(iOS 10, *)
      

      读作:如果 iOS 版本 10 或更高版本可用...

      因此,如果您保留这样的代码并且 iOS 11、12 等即将推出,它们都会进入这个 if 分支。

      因此,只要 Push API 未更改,您的代码就可以工作。 如果 Apple 将来更改 API(比如在 iOS 12 中),您将不得不添加另一个 if-branch 并将您的应用重新提交到商店...

      (如果使用 iOS 9 的人使用您的应用程序,他们将在第二个 if 分支中结束 - 因为第一个不适用。所以 ifs 的顺序保证较低的 iOS 版本获得正确的代码.)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-16
        相关资源
        最近更新 更多