gpt4 book ai didi

node.js - firebase-admin:获取失败通知传递结果的 token

转载 作者:太空宇宙 更新时间:2023-11-03 22:04:50 28 4
gpt4 key购买 nike

假设我们要向两个注册 token (仅限 Android 设备,无 iOS)发送通知,如下所示:

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 的用户已从其设备中删除了该应用。因此,该 token 不再在 firebase 中注册。错误对象如下所示:

传送失败。显示结果:


{"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."
}
}

问题:我的问题是,我只知道其中一次交付失败了。但我不知道错误与哪个 token 相关。因此我无法从数据库中删除过时的 token 。

问题:有没有办法找出哪些代币交付失败?

Github问题链接:https://github.com/firebase/firebase-admin-node/issues/600

最佳答案

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

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

这看起来像是一种黑客攻击,但当我拥有单个用户的多个设备 token 时,它对我有用,因为每当他们登录时我都必须存储新的设备 token 。

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

});

此方法也用于 firbease sample :https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

关于node.js - firebase-admin:获取失败通知传递结果的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57161160/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com