Register For Remote Notifications With Firebase Outside AppDelegate

Here’s how you can ask a user to register for remote notification when a certain action has been taken, i.e. created a new account.

import Firebase
import UserNotifications
DispatchQueue.main.async() {
    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 = UIApplication.shared.delegate as! AppDelegate
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = UIApplication.shared.delegate as! AppDelegate
    } else {
        let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound]
        let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
        UIApplication.shared.registerUserNotificationSettings(pushNotificationSettings)
    }
    UIApplication.shared.registerForRemoteNotifications()
}
 

webjunkie

 

Leave a Reply

Your email address will not be published. Required fields are marked *