FCM - Not registered

前端 未结 4 738
無奈伤痛
無奈伤痛 2021-01-18 09:57

I\'m trying to send a message through FCM to my web app. Unfortunately, I\'ve the following error message and I\'m unable to solve it alone...

Through POSTMAN I rece

相关标签:
4条回答
  • 2021-01-18 10:23

    The error NotRegistered means that your web app is not registered or the key you are using in the field "to" is not correct. In your case I suspect of the fact that the key is written in 2 lines, and maybe the backspace is being send in the request. Try to send the "to" field key in a single line.

    0 讨论(0)
  • 2021-01-18 10:23

    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!!

    Citations-https://medium.com/@anum.amin/react-native-integrating-push-notifications-using-fcm-349fff071591

    0 讨论(0)
  • 2021-01-18 10:28

    I had a similar issue and spend long hours (even days) to fix it.

    I was doing tests on dev and select on XCode > Signing & Capabilities: "Automatically manage signing". When select this option you expect XCode to create App identifier for you and could see it on apple developer account prefixed by "XC". But in my case, I don't know why, this App identifier not exists, and build, run, works well. And, when I send a notification get "InvalidRegistration" the 1st time and "NotRegistered" next ones.

    To fix it I manually create an App identifier on apple developer account, uncheck/check "Automatically manage signing" on XCode and (re)build/install to raise a new fcm token. After that it works!

    0 讨论(0)
  • 2021-01-18 10:39

    The official firebase documentation says:

    An existing registration token may cease to be valid in a number of scenarios, including:

    • If the client app unregisters with FCM.
    • If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the APNS Feedback Service reported the APNS token as invalid.
    • If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNS token has expired for iOS devices).
    • If the client app is updated but the new version is not configured to receive messages.

    For all these cases, remove this registration token from the app server and stop using it to send messages.

    Usually you will receive this error in case when backend application sends a push with an expired token.

    0 讨论(0)
提交回复
热议问题