这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

bug: No such module 'FirebaseInstanceID' - can't initialize push notifications on iOS #295

@simo996

Description

@simo996

Bug Report

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 2.4.7
  @capacitor/core: 2.4.7
  @capacitor/android: 2.4.7
  @capacitor/electron: 2.4.7
  @capacitor/ios: 2.4.7

Installed Dependencies:

  @capacitor/cli 2.4.7
  @capacitor/ios 2.4.7
  @capacitor/core 2.4.7
  @capacitor/android 2.4.7
  @capacitor/electron not installed

Platform(s)

iOS

Current Behavior

When following the tutorial to enable push notifications on iOS, to receive the notification token from Firebase Cloud Messaging (instead of APN) the guide states that you must

import FirebaseInstanceID // Add this line after import FirebaseCore
import FirebaseMessaging

After that you should change the function below (note that InstanceID reference that I believe is included in FirebaseInstanceID) in the AppDelegate.swift file:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: error)
            } else if let result = result {
                NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: result.token)
            }
        }
    }

However, following the instructions of the guide, Xcode shows the error "No such Module FirebaseInstanceID" when importing at the start of the AppDelegate.swift file

Expected Behavior

The module is imported correctly to enable handling of push notifications tokens from FCM instead of APN.

Code Reproduction

The AppDelegate.swift file

import UIKit
import Capacitor
import FirebaseCore
import FirebaseInstanceID // Add this line after import FirebaseCore
import FirebaseMessaging


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    return true
  }

  func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  }

  func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  }

  func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  }

  func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  }

  func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  }

  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // Called when the app was launched with a url. Feel free to add additional processing here,
    // but if you want the App API to support tracking app url opens, make sure to keep this call
    return CAPBridge.handleOpenUrl(url, options)
  }
  
  func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // Called when the app was launched with an activity, including Universal Links.
    // Feel free to add additional processing here, but if you want the App API to support
    // tracking app url opens, make sure to keep this call
    return CAPBridge.handleContinueActivity(userActivity, restorationHandler)
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)

    let statusBarRect = UIApplication.shared.statusBarFrame
    guard let touchPoint = event?.allTouches?.first?.location(in: self.window) else { return }

    if statusBarRect.contains(touchPoint) {
      NotificationCenter.default.post(CAPBridge.statusBarTappedNotification)
    }
  }

  #if USE_PUSH

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
    InstanceID.instanceID().instanceID { (result, error) in
      if let error = error {
        NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: error)
      } else if let result = result {
        NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: result.token)
      }
    }
  }

  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: error)
  }

#endif

}

The Podfile

platform :ios, '11.0'
use_frameworks!

# workaround to avoid Xcode caching of Pods that requires
# Product -> Clean Build Folder after new Cordova plugins installed
# Requires CocoaPods 1.6 or newer
install! 'cocoapods', :disable_input_output_paths => true

def capacitor_pods
  # Automatic Capacitor Pod dependencies, do not delete
  pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
  pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
  
  # Do not delete
end

target 'App' do
  capacitor_pods
  # Add your Pods here
    
  pod 'FirebaseCore'
  pod 'Firebase/Messaging'
end

Podfile.lock

PODS:
  - Capacitor (2.4.7):
    - CapacitorCordova
  - CapacitorCordova (2.4.7)
  - Firebase/CoreOnly (8.0.0):
    - FirebaseCore (= 8.0.0)
  - Firebase/Messaging (8.0.0):
    - Firebase/CoreOnly
    - FirebaseMessaging (~> 8.0.0)
  - FirebaseCore (8.0.0):
    - FirebaseCoreDiagnostics (~> 8.0)
    - GoogleUtilities/Environment (~> 7.4)
    - GoogleUtilities/Logger (~> 7.4)
  - FirebaseCoreDiagnostics (8.0.0):
    - GoogleDataTransport (~> 9.0)
    - GoogleUtilities/Environment (~> 7.4)
    - GoogleUtilities/Logger (~> 7.4)
    - nanopb (~> 2.30908.0)
  - FirebaseInstallations (8.0.0):
    - FirebaseCore (~> 8.0)
    - GoogleUtilities/Environment (~> 7.4)
    - GoogleUtilities/UserDefaults (~> 7.4)
    - PromisesObjC (~> 1.2)
  - FirebaseMessaging (8.0.0):
    - FirebaseCore (~> 8.0)
    - FirebaseInstallations (~> 8.0)
    - GoogleUtilities/AppDelegateSwizzler (~> 7.4)
    - GoogleUtilities/Environment (~> 7.4)
    - GoogleUtilities/Reachability (~> 7.4)
    - GoogleUtilities/UserDefaults (~> 7.4)
  - GoogleDataTransport (9.0.0):
    - GoogleUtilities/Environment (~> 7.2)
    - nanopb (~> 2.30908.0)
    - PromisesObjC (~> 1.2)
  - GoogleUtilities/AppDelegateSwizzler (7.4.1):
    - GoogleUtilities/Environment
    - GoogleUtilities/Logger
    - GoogleUtilities/Network
  - GoogleUtilities/Environment (7.4.1):
    - PromisesObjC (~> 1.2)
  - GoogleUtilities/Logger (7.4.1):
    - GoogleUtilities/Environment
  - GoogleUtilities/Network (7.4.1):
    - GoogleUtilities/Logger
    - "GoogleUtilities/NSData+zlib"
    - GoogleUtilities/Reachability
  - "GoogleUtilities/NSData+zlib (7.4.1)"
  - GoogleUtilities/Reachability (7.4.1):
    - GoogleUtilities/Logger
  - GoogleUtilities/UserDefaults (7.4.1):
    - GoogleUtilities/Logger
  - nanopb (2.30908.0):
    - nanopb/decode (= 2.30908.0)
    - nanopb/encode (= 2.30908.0)
  - nanopb/decode (2.30908.0)
  - nanopb/encode (2.30908.0)
  - PromisesObjC (1.2.12)

DEPENDENCIES:
  - "Capacitor (from `../../node_modules/@capacitor/ios`)"
  - "CapacitorCordova (from `../../node_modules/@capacitor/ios`)"
  - Firebase/Messaging
  - FirebaseCore

SPEC REPOS:
  trunk:
    - Firebase
    - FirebaseCore
    - FirebaseCoreDiagnostics
    - FirebaseInstallations
    - FirebaseMessaging
    - GoogleDataTransport
    - GoogleUtilities
    - nanopb
    - PromisesObjC

EXTERNAL SOURCES:
  Capacitor:
    :path: "../../node_modules/@capacitor/ios"
  CapacitorCordova:
    :path: "../../node_modules/@capacitor/ios"

SPEC CHECKSUMS:
  Capacitor: 4cd7b342d5a15b1f6f333d687358953afd7b2d8b
  CapacitorCordova: 2c9d6e28d9d86c4da087fa2c3537c1841954bb12
  Firebase: 73c3e3b216ec1ecbc54d2ffdd4670c65c749edb1
  FirebaseCore: 3f09591d51292843e2a46f18358d60bf4e996255
  FirebaseCoreDiagnostics: a31d987ba0fe16d59886a5dbadc2f1de871f88c8
  FirebaseInstallations: c4aab1005d6547b00a7529777fe52f5d4d45165b
  FirebaseMessaging: 1a33b4af3c8042ed6ddacb6c031894af2064bfab
  GoogleDataTransport: 11e3a5f2c190327df1a4a5d7e7ae3d4d5b9c9e4c
  GoogleUtilities: f8a43108b38a68eebe8b3540e1f4f2d28843ce20
  nanopb: a0ba3315591a9ae0a16a309ee504766e90db0c96
  PromisesObjC: 3113f7f76903778cf4a0586bd1ab89329a0b7b97

PODFILE CHECKSUM: 8dd6c5cf8d784e8fe9692f6ec35af7906b2c43ce

COCOAPODS: 1.10.1

Other Technical Details

IDE: VSCode 1.56.1 + Xcode 12.4
OS: MacOs BigSur 11.3.1

npm --version output: 7.12.1

node --version output: v15.12.0

pod --version output (iOS issues only): 1.10.1

Additional Context

I also tried following the guide of Simon Grimm at the following link that is very similar to the Capacitor's official guide;

  • the same error occurs
  • in that guide the only pod dependency added is 'Firebase/Messaging'
  • in the AppDelegate.swift file the only Firebase related import is 'import Firebase' just below 'import Capacitor'

Two years ago someone posted the following question on stack overflow and in those screenshots a folder 'FirebaseInstanceID' can be seen under the Pods folder in Xcode; in my project this folder is missing (and the project was scaffolded with an ionic capacitor starter project)

A similar error was posted at this link; I don't know native iOS development but I suppose that the issue is caused by the latest Firebase Cocoapods plugin releases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions