Register notification in iOS 9

久未见 提交于 2019-12-07 05:21:42

问题


I am using this to register notification :

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

But it's not woking for iOS 9.


回答1:


I have found solution :

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

The above code is working perfectly for me .



来源:https://stackoverflow.com/questions/33008072/register-notification-in-ios-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!