Firebase - Cannot assign value of type 'AppDelegate' to type 'UNUserNotificationCenterDelegate?'

11,451

At the end of AppDelegate you need to add 2 extensions (UNUserNotificationCenterDelegate, MessagingDelegate)

See the source code from this sample app: https://github.com/firebase/quickstart-ios/tree/master/messaging

Share:
11,451
user3427013
Author by

user3427013

Updated on June 23, 2022

Comments

  • user3427013
    user3427013 almost 2 years

    I always get compiling erros when deploying firebase using this tutorial: https://firebase.google.com/docs/cloud-messaging/ios/client My deployment SDK is 9.0.

    Errors I get:

    How can I fix this?

    Cannot assign value of type 'AppDelegate' to type 'UNUserNotificationCenterDelegate?'

    1st scenario - What I did step by step (following the tutorial):

    • pod init with the following: pod 'Firebase/Core' pod 'Firebase/Messaging'
    • pod install
    • "import Firebase" on top of AppDelegate class

    2nd scenario - Downloaded google demo iOS client app through a github repository ("messaging" folder under https://github.com/firebase/quickstart-ios)

    • compiled their app... worked fine.
    • compied to my existing XCode project their logic as according to the following steps:
    • pod init with the following: pod 'Firebase/Messaging'
    • pod install
    • imported the following: UIKit, UserNotifications, Firebase, FirebaseInstanceID, FirebaseMessaging

    Code in AppDelegate:

    func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // [START register_for_notifications]
            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 = self
                // For iOS 10 data message (sent via FCM)
                FIRMessaging.messaging().remoteMessageDelegate = self
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
            application.registerForRemoteNotifications()
            // [END register_for_notifications]
            FIRApp.configure()
            // Add observer for InstanceID token refresh callback.
            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(self.tokenRefreshNotification),
                                                   name: .firInstanceIDTokenRefresh,
                                                   object: nil)
            return true
        }
    
    
        
    
    • Surely
      Surely over 7 years
      did your AppDelegate class implements "UNUserNotificationCenterDelegate" protocol? If not, the compiler cant match the type and that is why gives the error.
    • user3427013
      user3427013 over 7 years
      The weird thing is that google's ios client example only implements UIResponder, UIApplicationDelegate... I've added UNUserNotificationCenterDelegate and FIRMessagingDelegate. Now, it says "AppDelegate" doesn't confirm with "FIRMessagingDelegate". I'm gonna research on this. Thanks.
    • user3427013
      user3427013 over 7 years
      Got it... The example has 2 extensions... lol But the tutorial doesn't have anything related to this. Anyways.
    • Hedylove
      Hedylove over 7 years
      @user3427013 how did you solve the "Now, it says "AppDelegate" doesn't confirm with "FIRMessagingDelegate"? I'm getting that error too.
    • user3427013
      user3427013 about 7 years
      @JozemiteApps see answer below.
    • Kunal Balani
      Kunal Balani about 4 years
      I had the same problem, in my case I was referring self in static func
  • Sam ツ
    Sam ツ about 5 years
    FIRMessagingDelegate has been renamed to MessagingDelegate