我们在 iOS 和 Android 中开发了一个应用程序,它将 FCM token 存储在数据库中,以便根据用户配置发送 PUSH 通知。
用户安装应用程序,每个设备的 token 都存储在数据库中,因此我们想知道这些 token 中有哪些是无效的,因为应用程序已被卸载。
另一方面,我们使用 JSON 通过网站发送通知。是否有任何限制(我的意思是,JSON 请求中的元素是否有任何限制)?
非常感谢!
最佳答案
我最近注意到 step 9 in the Cloud Functions codelab使用从 FCM API 获得的响应从其数据库中删除无效的注册 token 。
那里的相关代码:
// 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 中也使用了同样的方法。 .
关于android - 验证存储的 Firebase FCM token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49049399/