firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.6k stars 360 forks source link

FR: Divide "messaging/registration-token-not-registered" error into more specific parts. #533

Open SephReed opened 5 years ago

SephReed commented 5 years ago

I'm not sure if this is the correct repo for this pr, but it is the codebase I'm working in.

I'm currently running into the error messaging/registration-token-not-registered, and found the documentation on it:

The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons, including:

  • The client app unregistered itself from FCM.
  • The client app was automatically unregistered. This can happen if the user uninstalls the application or, on iOS, if the APNS Feedback Service reported the APNS token as invalid.
    • The registration token expired. For example, Google might decide to refresh registration tokens or the APNS token may have expired for iOS devices.
    • The client app was updated, but the new version is not configured to receive messages. For all these cases, remove this registration token and stop using it to send messages.

Given the documentation above, it would seem this error is quite broad, requiring an in-depth understanding of "app registration to FCM," "APNS tokens," "Expiration," and mis-configured apps. I'm pretty uncertain where to start in terms of approaching it.

As a Feature Request, I think it would be really, really helpful if this one mega error was instead a few small ones, for instance:

hiranya911 commented 5 years ago

SDK error codes aren't really hierarchical. They just look that way because they have the general form <service prefix>/<error code>. Plus I don't think the FCM service currently sends enough details in the error to distinguish between above 4 cases. So perhaps what we need a set of new error codes.

But we'll want the FCM backend service to support these first.

SephReed commented 5 years ago

Is there anywhere I should re-post this issue? It's has become a huge blocker for me.

PurnimaNaik commented 5 years ago

This doesn't exactly match your case but see if it helps-

I was facing the same issue. Here is how I fixed it (iOS specific, android was working out of the box)-

  1. Uninstall the app from your phone
  2. Clear ALL the tokens stored in your database for this particular account(or however you have set it up in your backend. In my case, tokens are tied to the account)
  3. In componentDidmount-
      async componentDidMount() {
        this.checkPermission();
        this.createNotificationListeners();
      }

4.

  async checkPermission() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
      this.getToken();
    } else {
      this.requestPermission();
    }
  }

  async requestPermission() {
    try {
      await firebase.messaging().requestPermission();
      // User has authorised
      this.getToken();
    } catch (error) {
      // User has rejected permissions
      console.log('permission rejected');
    }
  }

  async getToken() {
    try {
      const enabled = await firebase.messaging().hasPermission();
      if (!enabled) {
        await firebase.messaging().requestPermission();
      }

      const fcmToken = await firebase.messaging().getToken();
      if (fcmToken) {
        console.log("got token");
        console.log('fcm token:', fcmToken); //-->use this token from the console to send a post request via postman
        this.setState({ fcmToken });
        return fcmToken;
      }
    } catch (error) {
      console.warn('notification token error', error);
    }
  }

5.

 async createNotificationListeners() {
    /*
    * Triggered when a particular notification has been received in foreground
    * */
    this.notificationListener = firebase.notifications().onNotification((notification) => {
        const { title, body } = notification;
        this.showAlert(title, body);
    });

    /*
    * If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
    * */
    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
        const { title, body } = notificationOpen.notification;
        this.showAlert(title, body);
    });

    /*
    * If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
    * */
    const notificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
        const { title, body } = notificationOpen.notification;
        this.showAlert(title, body);
    }
    /*
    * Triggered for data only payload in foreground
    * */
    this.messageListener = firebase.messaging().onMessage((message) => {
      //process data message
      console.log(JSON.stringify(message));
    });
  }
  1. Finally, use the token that you see in the console as a value for the "to" key in your post request. You should no longer see the "notRegistered" error and should be getting push notifications. Done!!

For a more detailed setup/citation- https://medium.com/@anum.amin/react-native-integrating-push-notifications-using-fcm-349fff071591

SephReed commented 5 years ago

I'll have to try this out. I'm currently using nativescript, so it'll end up being a bit different, but I should still be able to apply a lot of this logic. Thank you very much!

sgrigorev commented 5 years ago

@SephReed, where you found that registration token can expire? I can't find it in the documentation.

SephReed commented 5 years ago

Whatever it was that was wrong, fixed itself. Didn't change anything, just started working. My guess is that it was the messaging/registration-token-not-registered/apns-invalid error.

dibikhin commented 5 years ago

@sgrigorev https://firebase.google.com/docs/cloud-messaging/send-message#admin_sdk_error_reference ctrl+f messaging/registration-token-not-registered

alexanderwallin commented 4 years ago

I don't know the exact contents of the raw API response, but implementing https://github.com/firebase/firebase-admin-node/issues/756 might be helpful here.