2017-08-31 13 views
0

現在、私は基本的に通知を送信するための許可をユーザーに求め、その後、通知が有効になっているかどうかを確認するためにチェックして、次のコードを、持っている:許可を求めた後、UNNotificationsが有効になっているかどうかを確認するにはどうすればよいですか?

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in 
     if granted { 

     } else { 

     } 
    }) 

    let notifType = UIApplication.shared.currentUserNotificationSettings?.types 
    if notifType?.rawValue == 0 { 
     print("being called") 
     let alert = UIAlertController(title: "Notifications are disabled for this app.", message: "Please enable notifications for the app to work properly. This can be done by going to Settings > Notifications > NAME HERE and allow notifications.", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) 
     show(alert, sender: self) 
    } else { 
     //enabled 
    } 

コードの動作、通知が有効になっている場合しかし、それはチェックユーザーが「はい」または「いいえ」を選択できるようになります。したがって、何が選択されても、ダイアログボックスがポップアップします。ユーザーが「はい」または「いいえ」を選択するまで、承認ステータスを確認するために待つ方法はありますか?

答えて

2

あなたは、コールバックにチェックを移動することができ、その承認を最初にチェックされている:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in 
    if (granted) { 
     // Alert here 
    } else { 
     // Or here 
    } 
}) 
+0

うわー、それは、おかげで簡単に修正しました! –

関連する問題