2017-10-10 43 views
3

在我的應用中,我希望能夠檢查用戶是否啓用了通知。在iOS 10中,我使用委託中的檢查來完成此操作。檢查UILocalNotification棄用後是否啓用用戶通知

此檢查現在已經過時,我希望更新它,但我想不出在iOS的11

棄用警告如下的內容:

currentUserNotificationSettings'被棄用iOS的10.0:使用 UserNotifications Framework的 - [UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:]和 - [UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

我試圖用這個警告的幫助來更新代碼,但我無法弄清楚。

如果任何人可以建議無論如何得到這樣的支票工作,這將有很大幫助。我一直用於iOS 10的代碼如下,謝謝。

let notificationType = UIApplication.shared.currentUserNotificationSettings!.types 
if notificationType == [] { 
    print("Notifications are NOT enabled") 
} else { 
    print("Notifications are enabled") 
} 

回答

8

步驟1:import UserNotifications

步驟2:

UNUserNotificationCenter.current().getNotificationSettings { (settings) in 
    if settings.authorizationStatus == .authorized { 
    // Notifications are allowed 
    } 
    else { 
    // Either denied or notDetermined 
    } 
} 

檢查設置更詳細的信息的對象。

+0

你怎麼預iOS的10處理呢? – Josh

+0

您必須通過'currentUserNotificationSettings'方法從** UIApplication.shared **中獲取'UIUserNotificationSettings'對象。 – McNight

+0

非常感謝:) – Josh

3

第一步: - 您必須添加頭文件作爲

import UserNotifications 

我用checkpushNotification方法來檢查用戶是否啓用通知與否。使用從AppDelegate類的didFinishLaunchingWithOptions中調用此方法。希望它會幫助你,如果有任何問題,然後下面評論。

最後一步: -

func checkPushNotification(){ 
     if #available(iOS 10.0, *) { 
      UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in 

       switch setttings.authorizationStatus{ 
       case .authorized: 

        print("enabled notification setting") 

       case .denied: 

        print("setting has been disabled") 

       case .notDetermined: 
        print("something vital went wrong here") 
       } 
      } 
     } else { 

      let isNotificationEnabled = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) 
      if isNotificationEnabled{ 

       print("enabled notification setting") 
      }else{ 

       print("setting has been disabled") 
      } 
     } 
    } 
+0

和早期版本? – Josh

+0

@Josh我已經更新了上面的早期版本。如果它不起作用,請在下面評論。 –

+0

@ Amrit Tiwari是否意味着如果isNotificationEnabled = false,它會被拒絕?它是否和你的例子中的早期開關案例一樣? – Josh

相關問題