2016-07-25 71 views
5

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

回答

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/

+0

我想補充的是,'UIApplication.shared.registerForRemoteNotifications()'應該做的事之後'理所當然== TRUE' – KVISH