invertase / react-native-firebase

šŸ”„ A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.56k stars 2.19k forks source link

[šŸ›] The operation couldnā€™t be completed. No APNS token specified before fetching FCM Token #6893

Open k2xl opened 1 year ago

k2xl commented 1 year ago

Issue

Trying to get token from user.

import messaging from '@react-native-firebase/messaging';
import { registerRootComponent } from 'expo';
import React, { useEffect } from 'react';
import {
  Text
} from 'react-native';

async function onAppBootstrap() {
  // Register the device with FCM
  console.log('A');
  await messaging().registerDeviceForRemoteMessages();
  console.log('B');
  // Get the token
  const token = await messaging().getToken();

  // Save the token
  console.log('TOKEN ' + token);
}

function App() {
  useEffect(() => {
    onAppBootstrap();
  }, []);

  return (
    <Text>Hi</Text>
  );
}

console.log('Registering root component');
registerRootComponent(App);
export default App;

When running this on iOS simulator, I get A on the console but it never reaches B. On further debugging just calling await messaging() seems to do enough to hang.

I see now after adding initializeApp() with credentials from firebase the following error

The operation couldnā€™t be completed. No APNS token specified before fetching FCM Token

Project Files

Javascript

Click To Expand

#### `package.json`: ```json "@react-native-firebase/app": "^17.0.0", "@react-native-firebase/messaging": "^17.0.0", ``` #### `firebase.json` for react-native-firebase v6: ```json # N/A ```

iOS

Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like: ```ruby # N/A ``` #### `AppDelegate.m`: ```objc // N/A ```


Android

Click To Expand

#### Have you converted to AndroidX? - [ ] my application is an AndroidX application? - [ ] I am using `android/gradle.settings` `jetifier=true` for Android compatibility? - [ ] I am using the NPM package `jetifier` for react-native compatibility? #### `android/build.gradle`: ```groovy // N/A ``` #### `android/app/build.gradle`: ```groovy // N/A ``` #### `android/settings.gradle`: ```groovy // N/A ``` #### `MainApplication.java`: ```java // N/A ``` #### `AndroidManifest.xml`: ```xml ```


Environment

Click To Expand

System: OS: macOS 12.5 CPU: (10) arm64 Apple M1 Pro Memory: 227.88 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 19.5.0 - /opt/homebrew/bin/node Yarn: Not Found npm: 9.3.1 - /opt/homebrew/bin/npm Watchman: Not Found Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1 Android SDK: Not Found IDEs: Android Studio: 2022.1 AI-221.6008.13.2211.9477386 Xcode: 14.2/14C18 - /usr/bin/xcodebuild Languages: Java: 17.0.6 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.1.0 => 18.1.0 react-native: 0.70.5 => 0.70.5 react-native-macos: Not Found npmGlobalPackages: *react-native*: Not Found ``` OUTPUT GOES HERE ``` - **Platform that you're experiencing the issue on**: - [ ] iOS - [ ] Android - [x ] **iOS** but have not tested behavior on Android - [ ] **Android** but have not tested behavior on iOS - [ ] Both - **`react-native-firebase` version you're using that has this issue:** - `"@react-native-firebase/app": "^17.0.0", - "@react-native-firebase/messaging": "^17.0.0",` - **`Firebase` module(s) you're using that has the issue:** - `e.g. Instance ID` - **Are you using `TypeScript`?** - `Y`


ademirtemur commented 4 months ago

i think, it is your xcode version, if you use xcode 15.2 or highest xcode. you can try after upgrade the mobile device's software(OS) version. Install software(OS) updates on the device.

Klyado commented 4 months ago

I'm having the same issue here. @ademirtemur Could you please give more details? Not sure to understand your solution.

DarrKing commented 4 months ago

100% Fixed my issue by:

  1. setting FirebaseAppDelegateProxyEnabled to NO in Info.plist
  2. Go to AppDelegate.mm and add the following inside didFinishLaunchingWithOptions function: [application registerForRemoteNotifications];
  3. Go to didRegisterForRemoteNotificationsWithDeviceToken function and add this: [FIRMessaging messaging].APNSToken = deviceToken;
  4. Clean build and start build again.

(FYI, make sure to use real device for testing.)

Hope it works for you guys.

FYI: If you are using Firebase dynamic links for your project you can just switch the FirebaseAppDelegateProxyEnabled to YES as it causes issues with dynamic links.

This worked well.

FabricioAllves commented 4 months ago

Obrigado pelas respostas. Estou em um aplicativo gerenciado pela Expo, entĆ£o nĆ£o tinha um arquivo firebase.json...

Tentei adicionar um firebase.json na raiz da pasta e removi await messaging().registerDeviceForRemoteMessages()(pois estava mostrando que era desnecessƔrio).

No entanto await messaging().getToken(); trava... e ainda consigoThe operation couldnā€™t be completed. No APNS token specified before fetching FCM Token

TambƩm estou nesse mesmo cenƔrio

github-actions[bot] commented 3 months ago

Hello šŸ‘‹, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

a-eid commented 3 months ago

I'm getting this error

Error: [messaging/unknown] The data couldnā€™t be read because it isnā€™t in the correct format.

on ios ( using sentry ).

not sure if it's related here ?

tristanheilman commented 2 months ago

Not sure if this provides any insights. I've been working with language translation on my react native app and have found that whenever I change the preferred/set language of my iOS device and open my RN app, the messaging().getToken() FCM token fetch fails with this error code:

[Error: [messaging/unknown] The operation couldnā€™t be completed. No APNS token specified before fetching FCM Token]

The issue tends to resolve itself by attempting to restart the app a few times. Are there any specifics to why this would occur? Does modifying device settings invalidate an APNS token? Very confused why switching from English -> Spanish would cause this reproducible bug so easily.

Jiroge commented 2 months ago

I closed the emulator, used "npx expo run:ios" again, and now it works. I think this will help.

yuriiburov commented 1 month ago

After reading your comment, I immediately switched to React-native-bootsplash. Currently, I do not receive this error: "No APNS token specified before fetching FCM Token". Thank you.

I'm using the same package, but still this error happens

mhassanmalik commented 1 month ago

My issue was resolved by adding onRegister() inside PushNotification.configure(). I was not setting the retrieved APNS token in messaging() before asking for the FCM token.

onRegister(token) {
  if (Platform.OS === 'ios') {
    messaging().setAPNSToken(token.token);
  }
}
chrisl777 commented 1 month ago

If you're winding up on this thread, and have an Expo managed project, note that there's apparently an issue under Expo v51 where it removes a necessary entitlement, you can add it back in the app.config.ts:

ios: {
  entitlements: {
    "aps-environment": "development",
},

See: https://github.com/invertase/react-native-firebase/issues/7825

shail-spreadd commented 3 weeks ago

For me, by changing below:

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

To:

<key>FirebaseAppDelegateProxyEnabled</key>
<true/>

Worked

mattdeveloper commented 3 weeks ago

For me what solved was adding both "react-native-push-notification" and "@react-native-community/push-notification-ios" packages. Then in my entry file (root/index.js) adding this:

// ... other code ...

import PushNotification from 'react-native-push-notification';

PushNotification.configure({
  // Called when Token is generated (iOS and Android)
  onRegister: function (token) {
    console.log('TOKEN:', token);
    if (Platform.OS === 'ios') {
      messaging().setAPNSToken(token.token);
    }
  },
});

// ... other code...

AppRegistry.registerComponent(appName, () => App);
KimmoHernborg commented 2 weeks ago

I also suddenly got this problem, and none of the solutions in this thread helped me (I'm running on iOS simulator), a quick Erase all content and settings... solved the issue for me. šŸ¤·

4sskick commented 2 weeks ago

I also suddenly got this problem, and none of the solutions in this thread helped me (I'm running on iOS simulator), a quick Erase all content and settings... solved the issue for me. šŸ¤·

what version of firebase messaging & app are you using?

yilmar0309 commented 1 week ago

I solved by adding this code into AppDelegate.mm file. You can try this solution. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; //add this line }

This work for me, thanks

4sskick commented 1 week ago

I solved by adding this code into AppDelegate.mm file. You can try this solution. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; //add this line }

This work for me, thanks

can you please tell me what version of firebase app & messaging are you using?

yilmar0309 commented 1 week ago

I solved by adding this code into AppDelegate.mm file. You can try this solution. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; //add this line }

This work for me, thanks

can you please tell me what version of firebase app & messaging are you using?

"@react-native-firebase/app": "^20.1.0", "@react-native-firebase/messaging": "^20.1.0",