首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 9中的注册通知

iOS 9中的注册通知
EN

Stack Overflow用户
提问于 2015-10-08 06:23:27
回答 1查看 4.2K关注 0票数 6

我用这个来注册通知:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-08 07:14:53

我找到了解决办法:

代码语言:javascript
复制
 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])
    }

上面的代码非常适合我。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33008072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档