如何在用户启用后从推送通知设置页面导航和返回

时间:2017-10-23 10:06:06

标签: swift3

我使用下面的代码来检测用户是否启用了推送通知。

**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()

请帮忙。

由于

1 个答案:

答案 0 :(得分:0)

您可以要求启用这样的推送通知:

the kernel centred around 0.2

要检查用户是否已授予权限,请使用以下命令:

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

        }
  }