1
votes

I use '@react-native-firebase/messaging' module to send notifications. On Android everything works fine, Following is the error log I get when I try const fcmToken = await firebase.messaging().getToken(); on ios device.

NativeFirebaseError: [messaging/unknown] The operation couldn’t be completed. (com.firebase.iid error 1001.)

I have already implemented "react-native-permissions" to grant notification permission.

My AppDelegate.m contains :

if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
 }

Should I add anything else to it? Any help or suggestion will be very helpful. Thanks in advance

2
please let me know, if not solved. - Ankit Patidar
@AnkitPatidar I have posted my solution, hope it helps - Metin Ilhan
See solution here by jacobp100 the Workaround.m - Oleg Dater

2 Answers

2
votes

You need to check and ask messaging permission before get the fcm token in iOS

 /**
   * Check is notification showing permission enabled if not ask the permission.
   */
   async checkFcmPermission() {
    firebase
      .messaging()
      .hasPermission()
      .then(enabled => {
        if (enabled) {
          // User has permissions
          this.getFcmToken(); // const fcmToken = await firebase.messaging().getToken();
        } else {
          // User doesn't have permission
          firebase
            .messaging()
            .requestPermission()
            .then(() => {
              // User has authorized
              this.getFcmToken(); // const fcmToken = await firebase.messaging().getToken();
            })
            .catch(error => {
              // User has rejected permissions
              console.log(
                'PERMISSION REQUEST :: notification permission rejected',
              );
            });
        }
      });
  }
1
votes

There are a lot of solutions to this issue. I post my answer since some StackOverflow users requested. Here is my code

  const sleep = ms => {
    return new Promise(resolve => setTimeout(resolve, ms));
  };

  async function requestPermissionForNotification() {
    try {
      const permission = await requestNotifications(["alert", "badge", "sound"]);
      if (permission.status === "granted") {
        setupNotification();
      }
    } catch (e) {
      console.log(e)
    }
  };
  async function setupNotification() {
    try {

      await sleep(5000)
      // TODO no need token for now, Will be used for future releases 
      const enabled = await firebase.messaging().hasPermission();
      await requestNotifications(['alert', 'badge', 'sound']);
      await firebase.messaging().registerForRemoteNotifications();
      const token = await firebase.messaging().getToken();
     
      firebase.messaging().onMessage(async (remoteMessage) => {
   
      });
    } catch (e) {
      console.log(e)

    }
  }

And Also I have added following to Info.plist

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

To sum up, I use react permissions to grant permission and the sleep method is to wait for firebase to be ready. Finally uninstalled app couple time to get a result. When It started working, I couldn't dare to touch the code since I spent days on this.