如何为 iOS 10 启用通知?

How do I enable notifications for iOS 10?

在 iOS9 中,我们会提示用户是否需要通知,并将他们引导至设置页面,以便他们可以为我们的应用启用通知权限。但是,如下所示,在 iOS 内的我的应用程序权限设置中没有启用通知的选项 10. 是否有我必须采取的新流程来使用适当的权限填充此列表?

代码

//check the notifications status. First the global settings of the device, then our stored settings
func checkGlobalNotifications() {
    let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    if grantedSettings!.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
        globalNotificationsEnabled = true
        //if global notifications are on, then check our stored settings (user)
        if CallIn.Settings.notificationsEnabled { notificationsOn() } else { notificationsOff() }
    }
    else {
        globalNotificationsEnabled = false
        //global notifications (iOS) not allowed by the user so disable them
        notificationsOff()
    }
}

iOS10引入了UNUserNotificationCenter,现在用于所有本地和推送通知。例如:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert])
    { (granted, error) in
        if granted == true{
            NSLog("Granted")
             UIApplication.shared.registerForRemoteNotifications()
        }
        if let error = error {
            NSLog("Error: \(error.description)")
        }
    }

您可以使用 getNotificationSettings()

检查设置

WWDC 视频:https://developer.apple.com/videos/play/wwdc2016/707/