F4Ke
F4Ke

Reputation: 1751

React native firebase : Error: [messaging/unknown] The operation couldn’t be completed. No APNS token specified before fetching FCM Token

I am trying to setup push notification on ios and android using Firebase/mesaging.

I have followed the setup part using the googleservice-info and so on... I have activated push notification and both Background mode capabilities we need. I pushed the APN key on firebase for the ios.

This is my code :

index.js


import { getMessaging } from '@react-native-firebase/messaging';
import firebase from '@react-native-firebase/app';

const credentials = {...}

if (!firebase.apps.length) {
    firebase.initializeApp(credentials).then((app) => {
        getMessaging(firebase).setBackgroundMessageHandler(async remoteMessage => {
            console.log('Message handled in the background!', remoteMessage);
        });
    })
}

Then in my app.tsx

  const getFcmToken = async () => {
    console.log("trying to get")
    try {
      const newFcmToken = await messaging().getToken() // HERE
      console.log('newFcmToken');
      console.log(newFcmToken);
   } ...
 }
  const authStatus = await messaging().requestPermission();
  if (!messaging().isDeviceRegisteredForRemoteMessages) {
    await messaging().registerDeviceForRemoteMessages();
  }

  const enabled =
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL;


  if (enabled) {
    console.log('Authorization status:', authStatus);
    await getFcmToken();
  }


package-lock.json


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

and then I get a

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

I try to add this in the info.plist

<key>FirebaseAppDelegateProxyEnabled</key>
    <true/>
    
<key>GCMessagingSenderId</key>
    <string>...</string> 
    ```


At some point I remembered succeeding having a token, I have one on the firebase dashboard.

Upvotes: 4

Views: 11719

Answers (4)

yusefturin
yusefturin

Reputation: 31

If you are using the iOS Simulator then close it with "Force Quit" and restart the react native application the error might go way in my case I have updated the following:

"@react-native-firebase/app": "^21.2.0" -> "^21.6.2"
"@react-native-firebase/messaging": "^21.6.0",-> "^21.6.2"

And Added the Analytics as well:

"@react-native-firebase/analytics": "^21.6.2",

Then at some point I got the errors:

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

Restarted the simulator and tried again I obtained the Permission and the Token as well and got the pushNotification to work as it was.

Upvotes: 3

Emilien
Emilien

Reputation: 1

The messaging library can't find the APN token. This message/problem can arise from several reasons. In my case, i solved it by adding the following line in one the function from ios/AppDelegate.mm:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    ...
    [FIRMessaging messaging].APNSToken = deviceToken;` // <== this line
    ...
}

Upvotes: -1

Rahul Tiwari
Rahul Tiwari

Reputation: 372

I fixed this issue by adding the Push Notification capability in the xcode.

Enable Push Notifications in Xcode

  • Open your project in Xcode.
  • Select your app target.
  • Go to the Signing & Capabilities tab.
  • Add Push Notifications. Add Background Modes and enable Remote notifications.

enter image description here

Also make sure you have added the APNS key to firebase cloud-messaging console.

Set Up APNs in Firebase

  • Log in to the Apple Developer Console.
  • Navigate to Certificates, Identifiers & Profiles.
  • Under Keys or Certificates, create and download an APNs Auth Key or Certificate.
  • Go to the Firebase Console:
    • Navigate to Project Settings > Cloud Messaging.
    • Upload the APNs key or certificate under iOS App Configuration.

Upvotes: 0

Suthat
Suthat

Reputation: 173

I spent 3 days to fixed it and it definitely work fine for me.

  1. You need to add more code to ios/AppDelegate.mm as follows:

       - (BOOL)application:(UIApplication *)application 
             didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
       {
         ...
         [FIRApp configure];
         [application registerForRemoteNotifications]; // <--- Add this Line
         ...
       }
    
  2. You need to add another function to ios/AppDelegate.mm too:

    - (void)application:(UIApplication *)application
     didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
     [FIRMessaging messaging].APNSToken = deviceToken;
    }
    

Upvotes: 5

Related Questions