2017-10-10 20 views
3

私のアプリでは、通知が有効かどうかをチェックできるようにしたい。 iOS 10では、デリゲートのチェックを使用してこれを行いました。UILocalNotificationの廃止後にユーザ通知が有効になっているかどうかをチェックする

このチェックは今、次のように非推奨の警告がある

非推奨と私はそれを更新したいが、私はiOSの11で使用するかを把握することはできませんされています

currentUserNotificationSettings'でしたそして - [UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

0:[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler] - UserNotifications Frameworkの使用:iOSの10.0で非推奨

この警告の助けを借りてコードを更新しようとしましたが、わかりません。

誰かがこのようなチェックを得るためにとにかく提案できれば、多くの助けになるでしょう。私が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

** UIApplication.shared **から 'currentUserNotificationSettings'メソッドを介して' UIUserNotificationSettings'オブジェクトを取得する必要があります。 – McNight

+0

大変ありがとう:) – Josh

3

第1工程: - あなたは

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

関連する問題