askaale askaale - 1 year ago 95
Swift Question

Swift FIRMessaging send push notification request on signup

I was wondering how I could prompt the user with the well known "'app' wants to send you push notifications" after the user has registered as a user on my app, and not as the user opens my app as default (without even being a user). How would I do this? I thought you would only configure push notification settings in the appDelegate.swift?

Thanks in advance!

Answer Source

You can ask for permission for push notification anywhere you want, where realistically if your app has a login page, then for sure after login you need to do it.

You will need first to place the code which asks for the permission in a function in AppDelegate.swift.

func registerUserNotification() {

        if #available(iOS 10.0, *) {
            let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]

            UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (bool, error) in

                UNUserNotificationCenter.current().delegate = self

                if (error == nil) {




        } else {
            if UIApplication.shared.responds(to: #selector(UIApplication.registerUserNotificationSettings(_:))) {

                let types:UIUserNotificationType = ([.alert, .badge, .sound])
                let settings:UIUserNotificationSettings = UIUserNotificationSettings(types: types, categories: nil)



Note: Also don't forget to import UserNotifications to be able to use the latest SDK for iOS 10.

in any view controller you need to reference the app delegate and call that function:

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

Update 1:

You will need to implement the delegate of UNUserNotificationCenter as the following, place the following code at the end of AppDelegate out of class scope (}), this is required for iOS 10 :

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        print("Userinfo \(notification.request.content.userInfo)")


    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        print("Userinfo \(response.notification.request.content.userInfo)")