假设我们想向两个registrationTokens (只有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注册的设备用户从他的设备中删除了该应用程序。因此,令牌不再在firebase中注册。error对象如下所示:
Delivery failed. Showing result:
{"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
发布于 2019-07-23 10:15:17
您需要在forEach中使用索引,并从您在sendToDevice中传递的数组中获取令牌。
官方文件:https://firebase.google.com/docs/reference/admin/node/admin.messaging.MessagingDevicesResponse
这似乎是一个黑客,但当我有一个用户的多个设备令牌,因为我必须存储新的任何时候,他们登录我的工作。
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。
https://stackoverflow.com/questions/57161160
复制相似问题