Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: no such module 'FirebaseInstanceID' on ios

i am trying to run my code and keep telling me

error: no such module 'FirebaseInstanceID'
    import FirebaseInstanceID

my code in AppDelegate.swift

import UIKit
import Flutter

import Firebase
import FirebaseAuth
import UserNotifications
import FirebaseInstanceID

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
  
   override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let firebaseAuth = Auth.auth()
        firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)

    }
    override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let firebaseAuth = Auth.auth()
        if (firebaseAuth.canHandleNotification(userInfo)){
            print(userInfo)
            return
        }
    
    }
}

i think is missing in my pod, i did not no how to install it to my pod file.

like image 315
Gbenga B Ayannuga Avatar asked May 14 '21 18:05

Gbenga B Ayannuga


People also ask

Why is my firebaseinstanceid not working in Swift?

The problem is that FirebaseInstanceID has recently became deprecated, so the above code is no longer working with the new version. FCM token is now provided directly on the Messaging instance. Modify the code above in your AppDelegate.swift to match the one below.

What is Instance ID in Firebase?

Firebase Instance ID provides a unique identifier for each app instance and a mechanism to authenticate and authorize actions (example: sending FCM messages). Instance ID is stable except when: App deletes Instance ID. App is restored on a new device. User uninstalls/reinstall the app. User clears app data.

How to get FCM token from Firebase Cloud Messaging?

If you have implemented Firebase Cloud Messaging for notifications, you had to add FirebaseInstanceID package in order to receive a FCM token. You also had to add the following code to your AppDelegate.swift file: The problem is that FirebaseInstanceID has recently became deprecated, so the above code is no longer working with the new version.

How stable is the Instance ID?

Instance ID is stable except when: Once an Instance ID is generated, the library periodically sends information about the application and the device where it's running to the Firebase backend. To stop this, see deleteInstanceId ().


Video Answer


2 Answers

FirebaseInstanceID is deprecated now, that is why this error is coming. Remove import FirebaseInstanceID from your AppDelegate.swift and instead add import FirebaseMessaging because FCM token is now provided directly on the Messaging instance. Now replace the code of InstanceID in didRegisterForRemoteNotificationsWithDeviceToken with this

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
    Messaging.messaging().token { (token, error) in
        if let error = error {
            print("Error fetching remote instance ID: \(error.localizedDescription)")
        } else if let token = token {
            print("Token is \(token)")
        }
    }
}
like image 116
Vincent Joy Avatar answered Oct 24 '22 13:10

Vincent Joy


updated 19/07/2021

remove the FirebaseInstanceID and install Firebase Messaging latest version..

i later solve the issue by installing Firebase Messaging Plugin

Firebase messaging Plugin

like image 42
Gbenga B Ayannuga Avatar answered Oct 24 '22 13:10

Gbenga B Ayannuga