Search code examples
iosfirebaseswift3firebase-cloud-messagingremote-notifications

How to deal with delegation of UNUserNotificationCenter and FIRMessaging w/ Firebase in Swift 3?


I'm trying to implement push notifications into my application with Firebase, however when following their documentation I have to add the following line of code:

        if #available(iOS 10.0, *) {
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in})

        UNUserNotificationCenter.current().delegate = self
        FIRMessaging.messaging().remoteMessageDelegate = self
    } else {
        let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

Upon adding this code, the application will not compile, and it will throw an error at me letting me know that I

cannot assign value of type AppDelegate to type FirMessagingDelegate

and that I

cannot assign value of type AppDelegate to type UNUserNotificationCenterDelegate.

So naturally, I add the FIRMessagingDelegate and UNUserNotificationCenterDelegate to my application, however the FIRMessagingDelegate does not allow the application to compile saying that

type AppDelegate does not conform to protocol 'FirMessagingDelegate'.

Any ideas? I haven't managed to find any other situations in which others have encountered this error, and in Google's documentation, these two delegates aren't even added.


Solution

  • You are on the right track, 1 more thing to do is making your AppDelegate class conform to the 'FirMessagingDelegate' protocol, by implementing the applicationReceivedRemoteMessage function somewhere is your AppDelegate class as it is required by the 'FirMessagingDelegate' protocol described here Firebase Doc