firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.64k stars 1.48k forks source link

APNS device token not set before retrieving FCM Token for Sender ID #12445

Closed alstr closed 7 months ago

alstr commented 8 months ago

Description

I'm well aware this is a much discussed issue, but I've been struggling with this for weeks and I'm getting nowhere with troubleshooting it.

On launch of my iOS app, whether in the Simulator or on a real device, I get:

APNS device token not set before retrieving FCM Token for Sender ID...

Some sources say this is a benign issue, but alongside this I cannot get notifications to work on iOS, so I'm wondering if this is the cause or just a coincidence. It specifically says Notifications to this FCM Token will not be delivered over APNS which is the issue I am experiencing.

Reproducing the issue

The code is virtually a like-for-like reproduction of the integration instructions:

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

        Messaging.messaging().delegate = self

        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
          options: authOptions,
          completionHandler: { _, _ in }
        )

        application.registerForRemoteNotifications()

        return true
    }

Firebase SDK Version

9.6.0

Xcode Version

15.2

Installation Method

Swift Package Manager

Firebase Product(s)

Messaging

Targeted Platforms

iOS

Relevant Log Output

9.6.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '123456789'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
```json { "pins" : [ { "identity" : "abseil-cpp-swiftpm", "kind" : "remoteSourceControl", "location" : "https://github.com/firebase/abseil-cpp-SwiftPM.git", "state" : { "revision" : "583de9bd60f66b40e78d08599cc92036c2e7e4e1", "version" : "0.20220203.2" } }, { "identity" : "boringssl-swiftpm", "kind" : "remoteSourceControl", "location" : "https://github.com/firebase/boringssl-SwiftPM.git", "state" : { "revision" : "dd3eda2b05a3f459fc3073695ad1b28659066eab", "version" : "0.9.1" } }, { "identity" : "firebase-ios-sdk", "kind" : "remoteSourceControl", "location" : "https://github.com/firebase/firebase-ios-sdk", "state" : { "revision" : "7e80c25b51c2ffa238879b07fbfc5baa54bb3050", "version" : "9.6.0" } }, { "identity" : "googleappmeasurement", "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleAppMeasurement.git", "state" : { "revision" : "c1cfde8067668027b23a42c29d11c246152fe046", "version" : "9.6.0" } }, { "identity" : "googledatatransport", "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleDataTransport.git", "state" : { "revision" : "a732a4b47f59e4f725a2ea10f0c77e93a7131117", "version" : "9.3.0" } }, { "identity" : "googleutilities", "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleUtilities.git", "state" : { "revision" : "bc27fad73504f3d4af235de451f02ee22586ebd3", "version" : "7.12.1" } }, { "identity" : "grpc-ios", "kind" : "remoteSourceControl", "location" : "https://github.com/grpc/grpc-ios.git", "state" : { "revision" : "8440b914756e0d26d4f4d054a1c1581daedfc5b6", "version" : "1.44.3-grpc" } }, { "identity" : "gtm-session-fetcher", "kind" : "remoteSourceControl", "location" : "https://github.com/google/gtm-session-fetcher.git", "state" : { "revision" : "5ccda3981422a84186387dbb763ba739178b529c", "version" : "2.3.0" } }, { "identity" : "ios-maps-sdk", "kind" : "remoteSourceControl", "location" : "https://github.com/googlemaps/ios-maps-sdk", "state" : { "branch" : "main", "revision" : "dd5bd270e16a9fd4d309e238dc0e05fe5c26d978" } }, { "identity" : "ios-places-sdk", "kind" : "remoteSourceControl", "location" : "https://github.com/googlemaps/ios-places-sdk", "state" : { "revision" : "db961cdf29b116c497baf87c1ef8188082089fb7", "version" : "8.3.0" } }, { "identity" : "leveldb", "kind" : "remoteSourceControl", "location" : "https://github.com/firebase/leveldb.git", "state" : { "revision" : "9d108e9112aa1d65ce508facf804674546116d9c", "version" : "1.22.3" } }, { "identity" : "nanopb", "kind" : "remoteSourceControl", "location" : "https://github.com/firebase/nanopb.git", "state" : { "revision" : "819d0a2173aff699fb8c364b6fb906f7cdb1a692", "version" : "2.30909.0" } }, { "identity" : "promises", "kind" : "remoteSourceControl", "location" : "https://github.com/google/promises.git", "state" : { "revision" : "e70e889c0196c76d22759eb50d6a0270ca9f1d9e", "version" : "2.3.1" } }, { "identity" : "swift-protobuf", "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-protobuf.git", "state" : { "revision" : "65e8f29b2d63c4e38e736b25c27b83e012159be8", "version" : "1.25.2" } } ], "version" : 2 } ```

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
```yml Replace this line with the contents of your Podfile.lock! ```
google-oss-bot commented 8 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

rizafran commented 8 months ago

Hi @alstr, per checking, it seems that this issue has been resolved in v10.6.0. Have you tried to update your SDK and see if the issue will persist?

alstr commented 8 months ago

It persists as of 10.21.0.

The only difference is the addition of the message Declining request for FCM Token since no APNS Token specified, yet the FCM Token is still generated...

I even tried disabling auto init but neither FirebaseMessagingAutoInitEnabled = NO nor Messaging.messaging().isAutoInitEnabled = false are honoured. (Side note, on the latter, the docs are out of date).

At the moment it seems the SDK is almost completely broken on iOS. I haven't found anything that works remotely as expected, whereas the Android SDK was fine.

nguyenvanan18031993 commented 8 months ago

Hi @alstr , I got the same error as you. The push notification on my App is gone too.

alstr commented 7 months ago

I figured out the cause of the APNS device token not set before retrieving FCM Token for Sender ID... error.

In the initial view controller I was calling a function in viewDidLoad that included the following:

Messaging.messaging().token { token, error in
  if let error = error {
    print("Error fetching FCM registration token: \(error)")
  } else if let token = token {
    print("FCM registration token: \(token)")
    self.fcmRegTokenMessage.text  = "Remote FCM registration token: \(token)"
  }
}

This was apparently retrieving the FCM token before the APNS device token was set, so there was some kind of race condition between that and whatever is happening in AppDelegate.

I'm not entirely sure what the best practice is as the docs don't really say. I delayed calling the function until viewDidAppear and that silenced the error.

Messaging.messaging().token didn't seem to error when Firebase complains about the APNS token not being set, at least not in the version I was using originally.

I still think there's a few weird things going on as I mentioned above. Notifications started coming through but I'm not sure why exactly.

I'm a bit confused, but it's kind of working for now.