iosswiftpush-notificationpermissionsurbanairship.com

UrbanAirship iOS SDK - Is There A Notifications Permission Request Callback?


During my App's onboarding flow I have a page asking the user if they want to allow notification alerts (via UrbanAirship).

On either the user pressing "Don't Allow" or "Allow" in the subsequent SDK generated UIAlert I would like to perform some actions. In this case, transitioning to the next page of the onboarding flow when a user presses one of the two alert buttons. Furthermore, in the "Allow" case, update a variable in local storage to indicate that the user has previously given permission for push notifications.

Ideally I'd like to be able to implement a closure/callback for each of these button presses but I haven't been able to find out how to do this from the UrbanAirship docs.

Currently I have the following:

@IBAction func permissionsButtonPressed(_ sender: Any) {
        UAirship.push().userPushNotificationsEnabled = true
        UAirship.push().defaultPresentationOptions = [.alert, .badge, .sound]
        UAirship.push().resetBadge()
        UAirship.push().isAutobadgeEnabled = true

        //Update a variable in local storage
        // ...

        performSegue(withIdentifier: "exampleIdentifier", sender: self)
}

But this has the problem of instantiating the page transition and then presenting the notifications alert on top of the next page in the onboarding flow.

The closest thing to a solution that I've found in the UrbanAirship docs + forums is this forum question from 2017, but this only seems to give a possible "closure" solution for the "allow" button-pressed case (via didRegisterForRemoteNotifications), whereas I still want to be able to progress my users through onboarding even if they deny notifications permissions.

Ideally, I'd like to be able to implement completion closures similar to what is possible with a standard UIAlertController, for example:

func setupPermissionsAlert() {
    let alert = UIAlertController(title: "This App Would Like to Send You Notifications", message: "An Interesting and Informative Message", preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "Don't Allow", style: UIAlertAction.Style.default, handler: { [unowned self] action in
        //Some action
        self.notAllowedCompletion()
    }))
    alert.addAction(UIAlertAction(title: "Allow", style: UIAlertAction.Style.default, handler: { [unowned self] action in
        //Another action
       self.allowedCompletion()
    }))

    //N.B. topMostViewController() is a custom method to locate the topMostViewController presented
    UIApplication.shared.topMostViewController()?.present(alert, animated: true, completion: nil)
}

My question is, therefore, is this possible with UrbanAirship for BOTH the permission granted and rejected outcomes from the SDK presented alert? If so, how do I do it?


Solution

  • You can use enableUserPushNotifications:

    UAirship.push().enableUserPushNotifications({ (result) in
      // handle result
    })
    

    That will cause the system prompt for the permission and give you the result true if notifications are enabled, otherwise false.