Parameter of ‘messaging(_:didReceiveRegistrationToken:)’ has different optionality than required by protocol ‘MessagingDelegate’

Viewed 2767

This happens to me when I try to compile my app in XCode, in android it runs perfect, but there, it fails with that, this is the function that I use directly from Firebase cloud.

func messaging(_ messaging: Messaging, didReceiveRegistrationToken 
fcmToken: String) {
print("Firebase registration token: \(fcmToken)")

let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), 
object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a 
new 
token is generated.
}

This is where the error mentioned in the title of my question marks me, if someone has happened to him and he can give me a hand, since I have not seen a similar error here.

3 Answers

In addition to @Rashid's answer, you will also need to update the dataDict within the messaging function.

Simply add ?? ""

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
  print("Firebase registration token: \(String(describing: fcmToken))")

  let dataDict:[String: String] = ["token": fcmToken ?? ""]
  NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
  // TODO: If necessary send token to application server.
  // Note: This callback is fired at each app startup and whenever a new token is generated.
}

This is straight from the documentation: https://firebase.google.com/docs/cloud-messaging/ios/client

With the latest Firebase Messaging (7.0.0), the function has been updated to have optional fcmToken. Updating it to following would help:

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?)

Try this:

optional func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
Related