在iOS 9中注册通知

6
我正在使用这个来注册通知:
if application.respondsToSelector("registerUserNotificationSettings:") {
                let types:UIUserNotificationType = (.Alert | .Badge | .Sound)
                let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

                application.registerUserNotificationSettings(settings)
                application.registerForRemoteNotifications()

            } else {
                // Register for Push Notifications before iOS 8
                application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
            }

但是它在iOS 9上不能工作。

1个回答

10

我已经找到了解决方案:

 if application.respondsToSelector("registerUserNotificationSettings:") {
        if #available(iOS 8.0, *) {
            let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])
            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        } else {
            application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
        }
    }
    else {
        // Register for Push Notifications before iOS 8
        application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
    }

以上代码对我来说完美运行。


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