如何在iOS 10上启用通知?

5
在 iOS9 中,我们会提示用户是否需要通知,并引导他们到设置页面以便启用应用的通知权限。然而,在 iOS10 中,如下图所示,在我的应用程序权限设置中没有启用通知的选项。我是否需要采取新的流程来填充具有适当权限的列表?

enter image description here

代码

//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()
    }
}
1个回答

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

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

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


我想补充一点,UIApplication.shared.registerForRemoteNotifications() 应该在 granted == true 之后执行。 - KVISH

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