2017-10-23 13 views
-1

以下のコードを使用して、ユーザーがプッシュ通知を有効にしているかどうかを検出しました。ユーザーが有効にした後にプッシュ通知設定ページに戻る方法

**Problem**. 
1) How to open or navigate to the Push Notification setting Page in the phone 
2) How to return from this Push Notification page after user enabled it or 
    how user return to previous page if decide to enable later. 

VC_Check --> Push Notification settings 

in VC_check: 

if UIApplication.shared.isRegisteredForRemoteNotifications { 
      print("YES") 

    // goto other VC 

    } else { 

    // goto Phone setting page 

    } 


//-- I dont want this Pop Up to enable Push Notification: 

// detected not enabled, use below pop Up 

pageUIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 
    UIApplication.shared.registerForRemoteNotifications() 

助けてください。

おかげ

答えて

0

あなたはこのようにプッシュ通知を有効にするように依頼することができます

func requestNotificationPermission() { 
     let app = UIApplication.shared 

     // --- from right here 
     if #available(iOS 10.0, *) { 
      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization(
       options: authOptions, 
       completionHandler: {_, _ in }) 

      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.current().delegate = self 
      // For iOS 10 data message (sent via FCM) 

     } else { 
      let settings: UIUserNotificationSettings = 
       UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
      app.registerUserNotificationSettings(settings) 
     } 
     app.registerForRemoteNotifications() 
     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
       // Enable or disable features based on authorization. 
       // DO SOMETHING HERE AFTER USER AUTHORIZES, CALL A FUNCTION TO RELOAD VIEW? 
      } 
      app.registerForRemoteNotifications() 
     } else { 
      // Fallback on earlier versions 

     } 
    } 

は、ユーザーが許可した場合、許可使用を確認するには、この:

func checkNotificationStatus() { 
     let current = UNUserNotificationCenter.current() 

     current.getNotificationSettings(completionHandler: { (settings) in 
      if settings.authorizationStatus == .notDetermined { 
       // Notification permission has not been asked yet, go for it! 
       print("Not yet asked for permission") 
      } 

      if settings.authorizationStatus == .denied { 
       // Notification permission was previously denied, go to settings & privacy to re-enable 
       print("Permission denied") 
      } 

      if settings.authorizationStatus == .authorized { 
       // Notification permission was already granted 
       print("Permission Granted") 

      } 
     }) 
    } 
関連する問題