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

5

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

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

然后,我尝试向该令牌发送通知,但FCM服务器回复:Not Registered 错误!为什么会出现 Not Registered?当使用主题订阅时,Firebase是否不应该管理整个"令牌过程"并负责其更新?

有没有办法通知应用程序它已不再注册并采取适当的操作呢?谢谢。


你提到你的应用程序“读取Firebase注册令牌”。你是从哪里读取令牌的?你是从应用程序中存储令牌的某个位置读取它,还是调用了getToken()?此外,你是否知道用户卸载并重新安装应用程序或更新应用程序会使现有令牌无效? - Arthur Thompson
1
这个不应该在客户端处理吗?通过在onTokenRefresh()中再添加一个subscribeToTopic()来实现? - AL.
问题一直被报告:有时候一些用户抱怨通知不起作用;当我尝试向该用户的令牌发送通知时,答案是“未注册”。为什么??? :-( - mdicosimo
再次出现问题报告。他确认应用程序已安装,但当我发送通知时,答案是“未注册”。 - mdicosimo
不,我没有。看起来这种情况发生的频率较少,但我仍然有一些抱怨。最近的几个问题都是来自三星手机(最新型号)。你能分享一下你的经验吗? - mdicosimo
显示剩余10条评论
2个回答

1

这不完全适用于您的情况,但看看是否有帮助!我曾经遇到过同样的问题。以下是我如何解决它的方法(针对iOS特定情况,Android可以直接使用):

  1. 从手机中卸载应用程序
  2. 清除数据库中存储的与此特定账户相关的所有令牌(或者按照您在后端设置的方式进行操作。在我的情况下,令牌与账户绑定)
  3. 在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. 最后,在控制台中看到的令牌作为您的POST请求中“to”键的值。您不应再看到“notRegistered”错误,并且应该能够收到推送通知。 完成!

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


谢谢,但我的问题是关于Android的。我没有任何iOS方面的经验。无论如何,你的建议对于一些我不认识的用户来说实践起来相当困难,他们可能住在离我几公里远的地方;-) - mdicosimo

0
当用户卸载/重新安装应用程序后,旧令牌将不起作用,您需要代表旧令牌使用新令牌,在扩展FirebaseMessagingService类的情况下,请使用此重写方法。
public void onNewToken(@NonNull String token) {
    Log.e("newtknn",token);
    super.onNewToken(token);
}

现在您可以在FCM服务中使用此令牌发送通知。谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接