FCM - 发送到主题和“未注册”错误

mdi*_*imo 5 android registration subscribe firebase firebase-cloud-messaging

我开发了一个 Android 应用程序,它订阅一个主题(subscribeToTopic()OnCreate主要活动中),然后接收我的服务器发送到该主题的通知。

问题是,有时有人抱怨通知不再送达。在这种情况下,我通常要求用户使用我的应用程序中包含的一个函数,该函数读取 Firebase 注册令牌并通过电子邮件将其发送给我(我通常不使用或存储它)。

然后,我尝试向该令牌发送通知,并且 FCM 服务器回答错误Not Registered!怎么会这样Not Registered?使用主题订阅时,Firebase 不应该管理整个“令牌流程”并负责其续订吗?

有什么方法可以通知应用程序它不再注册并采取适当的操作吗?谢谢。

Pur*_*aik 1

这并不完全适合您的情况,但看看是否有帮助!我面临着同样的问题。这是我修复它的方法(iOS 特定,Android 开箱即用)-

  1. 从手机中卸载该应用程序
  2. 清除数据库中存储的该特定帐户的所有令牌(或者您在后端设置的所有令牌。在我的情况下,令牌与该帐户绑定)
  3. 在组件中Didmount-

    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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

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));
    });
  }
Run Code Online (Sandbox Code Playgroud)
  1. 最后,使用您在控制台中看到的令牌作为发布请求中“to”键的值。您应该不再看到“notRegistered”错误,并且应该收到推送通知。完毕!!

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