如何为iOS 10启用通知?

Dec*_*nna 5 notifications ios swift ios10

在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()
    }
}
Run Code Online (Sandbox Code Playgroud)

Gru*_*kes 8

iOS 10引入了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)")
        }
    }
Run Code Online (Sandbox Code Playgroud)

您可以使用检查设置 getNotificationSettings()

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