Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestAuthorization for push outside the didFinishLaunchingWithOptions

Tags:

ios

ios10

swift3

For ios 10 i used this for registering the push notifications :

Registering for Push Notifications in Xcode 8/Swift 3.0?

Is there a way to ask for the requestAuthorization(options:[.badge, .alert, .sound]) outside the appdelegate and the func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

The reason i ask is because i don't want to present the pop up for push notifications after the user has used the app for a bit. Any ideas?

like image 277
Pavlos Avatar asked Nov 03 '16 19:11

Pavlos


2 Answers

Like @dan said it isn't necessary to request the notifications permission in the AppDelegate. You can do it wherever you want to. This is what you probably be doing for that.

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
    if error == nil {
        if success {
            print("Permission granted")
            // In case you want to register for the remote notifications
            let application = UIApplication.shared
            application.registerForRemoteNotifications()
        } else {
            print("Permission denied")
        }
    } else {
        print(error)
    }
}

And Remember

  1. to import the UserNotifications framework where you use this code.
  2. if you register for remote notifications you need to implement the didRegisterForRemoteNotificationsWithDeviceToken method in your AppDelegate
like image 76
Adeel Miraj Avatar answered Nov 14 '22 21:11

Adeel Miraj


The question for me is the pop up won't show again once user agreed or denied it. So we have to redirect users to Settings after that manually.

Here comes the code in Swift:

@IBAction func userDidClickButton(_ sender: Any) {

    // initialise a pop up for using later
    let alertController = UIAlertController(title: "TITLE", message: "Please go to Settings and turn on the permissions", preferredStyle: .alert)
    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }
        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
            // do something
            }
         }
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(settingsAction)

    // check the permission status
    UNUserNotificationCenter.current().getNotificationSettings () { settings in            
        switch settings.authorizationStatus {
        case .denied, .notDetermined:
            self.present(alertController, animated: true, completion: nil)
        case .authorized:
            // continue the stuff
            DispatchQueue.main.sync {
                // Update UI
            }
        }
    }
}
like image 40
Allen Avatar answered Nov 14 '22 19:11

Allen