验证存储的Firebase FCM令牌 [英] Verify stored Firebase FCM tokens

查看:107
本文介绍了验证存储的Firebase FCM令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have developed an app in iOS and Android which stores the FCM tokens in a database in order to send PUSH notifications depending on the user configuration.

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

Users install the app and the token of every device is stored in the database, so we would like to know what of those tokens are invalid because the app has been uninstalled.

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

On the other hand, we send the notifications through the website using JSON. Is there any limitation (I mean, is there any limit of elements in a JSON request)?

非常感谢!

推荐答案

我最近注意到 Cloud Functions代码实验室中的第9步使用从FCM API获得的响应从其数据库中删除无效的注册令牌.

I recently noticed that step 9 in the Cloud Functions codelab uses the response it gets from the FCM API to remove invalid registration tokens from its database.

此处的相关代码:

// 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);
    });
  }
});

我迅速检查了一下,云函数中也使用了这种方法发送通知的示例.

这篇关于验证存储的Firebase FCM令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆