验证存储的Firebase FCM令牌

问题描述 投票:0回答:1

我们在iOS和Android中开发了一个应用程序,它将FCM令牌存储在数据库中,以便根据用户配置发送PUSH通知。

用户安装应用程序并且每个设备的令牌都存储在数据库中,因此我们想知道这些令牌中的哪些令牌无效,因为该应用程序已被卸载。

另一方面,我们使用JSON通过网站发送通知。是否有任何限制(我的意思是,JSON请求中的元素是否有限制)?

非常感谢你!

android ios json firebase firebase-cloud-messaging
1个回答
3
投票

我最近注意到step 9 in the Cloud Functions codelab使用从FCM API获取的响应来从其数据库中删除无效的注册令牌。

那里的相关代码:

// Get the list of device tokens.
return admin.database().ref('fcmTokens').once('value').then(allTokens => {
  if (allTokens.val()) {
    // Listing all tokens.
    const tokens = Object.keys(allTokens.val());

    // Send notifications to all tokens.
    return admin.messaging().sendToDevice(tokens, payload).then(response => {
      // For each message check if there was an error.
      const tokensToRemove = [];
      response.results.forEach((result, index) => {
        const error = result.error;
        if (error) {
          console.error('Failure sending notification to', tokens[index], error);
          // Cleanup the tokens who are not registered anymore.
          if (error.code === 'messaging/invalid-registration-token' ||
              error.code === 'messaging/registration-token-not-registered') {
            tokensToRemove.push(allTokens.ref.child(tokens[index]).remove());
          }
        }
      });
      return Promise.all(tokensToRemove);
    });
  }
});

我很快检查了同样的方法也用在Cloud Functions sample for sending notifications

© www.soinside.com 2019 - 2024. All rights reserved.