首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >firebase-admin:获取失败通知传递结果的令牌

firebase-admin:获取失败通知传递结果的令牌
EN

Stack Overflow用户
提问于 2019-07-23 09:38:44
回答 1查看 1.6K关注 0票数 3

假设我们想向两个registrationTokens (只有Android设备,没有iOS)发送一个通知,如下所示:

代码语言:javascript
复制
const tokens = ['tokenA', 'tokenB'];

const payload = {badge: 1, title: 'Hello', body: 'world'};
const options = {priority: 'high',contentAvailable: false, timeToLive: 60 * 60 * 24};

const admin = FirebaseAdmin.initializeApp({/*config here...*/});

admin.messaging().sendToDevice(deviceTokens, payload, options)
  .then((response) => {

    response.results.forEach((deviceResult) => {
      if (deviceResult.error) {
        console.log('Delivery failed. Showing result:\n', deviceResult);
      }
    });

});

曾经在tokenB注册的设备用户从他的设备中删除了该应用程序。因此,令牌不再在firebase中注册。error对象如下所示:

Delivery failed. Showing result:

代码语言:javascript
复制
{"error":
  {
    "code":"messaging/registration-token-not-registered",
    "message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."
  }
}

问题:我的问题是,我只知道其中一个交付失败了。但我不知道这个错误与哪一个标记有关。因此,我不能从数据库中删除过时的令牌。

的问题:有没有办法找出哪些令牌的传递失败了?

Github发行链接:https://github.com/firebase/firebase-admin-node/issues/600

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-23 10:15:17

您需要在forEach中使用索引,并从您在sendToDevice中传递的数组中获取令牌。

官方文件:https://firebase.google.com/docs/reference/admin/node/admin.messaging.MessagingDevicesResponse

这似乎是一个黑客,但当我有一个用户的多个设备令牌,因为我必须存储新的任何时候,他们登录我的工作。

代码语言:javascript
复制
const tokens = ['tokenA', 'tokenB'];

const payload = {badge: 1, title: 'Hello', body: 'world'};
const options = {priority: 'high',contentAvailable: false, timeToLive: 60 * 60 * 24};

const admin = FirebaseAdmin.initializeApp({/*config here...*/});

admin.messaging().sendToDevice(deviceTokens, payload, options)
  .then((response) => {

    response.results.forEach((deviceResult,index) => {
      if (deviceResult.error) {
        let failedToken = tokens[index];
        // Now use this token to delete it from your DB, or mark it failed according to your requirements.
      }
    });

});

这种方法也适用于纤维素酶样品:https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57161160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档