如何为iOS 10启用通知?

问题描述:

在iOS9中,我们会提醒用户他们是否需要通知并将他们引导至设置页面,以便他们可以为我们的应用启用通知许可。但是,如下所示,在iOS 10中,我无法在我的应用程序权限设置中启用通知。是否有一个新流程可以用适当的权限填充此列表?如何为iOS 10启用通知?

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

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/

+0

我想补充的是,'UIApplication.shared.registerForRemoteNotifications()'应该做的事之后'理所当然== TRUE' – KVISH