hesh
hesh

Reputation: 65

ios10, Swift 3 and Firebase Push Notifications setup

I am trying to follow the example for push notification https://firebase.google.com/docs/cloud-messaging/ios/client

I am having issues on the appDelete.swift for example

<pre>
 extension AppDelegate : FIRMessagingDelegate {
   // Receive data message on iOS 10 devices.
    func applicationReceivedRemoteMessage(remoteMessage:           FIRMessagingRemoteMessage) {
      print("%@", remoteMessage.appData)
 }

}

the error is

Method 'applicationReceivedRemoteMessage(remoteMessage:)' has different argument names from those required by protocol 'FIRMessagingDelegate' ('applicationReceivedRemoteMessage')

I get the following errors on the screen

enter image description here

I have the following cocopods too

Analyzing dependencies
Downloading dependencies
Installing Firebase (3.7.1)
Installing FirebaseAnalytics (3.4.4)
Installing FirebaseCore (3.4.3)
Installing FirebaseInstanceID (1.0.8)
Installing FirebaseMessaging (1.2.0)
Installing GoogleIPhoneUtilities (1.2.1)
Installing GoogleInterchangeUtilities (1.2.2)
Installing GoogleSymbolUtilities (1.1.2)
Installing GoogleUtilities (1.3.2)
Generating Pods project
Integrating client project

Can someone tell me where I am going wrong? I saw the project fcm example and when I compile everything is ok.

Upvotes: 0

Views: 2141

Answers (1)

Todd Kerpelman
Todd Kerpelman

Reputation: 17523

The issue is that the sample code you're looking at is in Swift 2.3, and your project is in Swift 3.0.

There's a few ways to address this:

  1. Go ahead and use swift 2.3. You can do this by going to your Project, select Build Settings and change Use Legacy Swift Language Version to Yes.

  2. Ask Xcode to automatically update it for you. You can do this by going to Edit > Convert > To Current Swift Syntax... and it should hopefully address the issues you're seeing.

  3. Update the code manually. As you can see, Xcode generally knows what the updated code is "supposed" to be, but it looks like the method that's giving you trouble can be updated to something that looks a little like this:

    func application(application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
      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()
    
      FIRApp.configure()
    
      // Add observer for InstanceID token refresh callback.
      NotificationCenter.default.addObserver(self,
                                             selector: #selector(self.tokenRefreshNotification),
                                             name: NSNotification.Name.firInstanceIDTokenRefresh,
                                             object: nil)
    
      return true
    }
    
  4. Bug the Firebase Sample Code people and tell them, "Hey! You forgot to update this sample to Swift 3.0!" Luckily, I'm in a position to do that, so will tell them right now. :)

Upvotes: 1

Related Questions