[英]Removing no longer valid FCM tokens from a database
I am sending notification to mobile device for which I have fcm tokens.我正在向我拥有 fcm 令牌的移动设备发送通知。 On sending, I get a response that can for each fcm token have an error.发送时,我收到每个 fcm 令牌都可能有错误的响应。 I want to remove the invalid tokens that for sure will no longer work, but keep tokens that may have failed this time and could still work in the future.我想删除肯定不再有效的无效令牌,但保留这次可能失败但将来可能仍然有效的令牌。
I am using the node-gcm
package.我正在使用node-gcm
package。
What are the possible values for the error
field in the response of each token.每个标记的响应中error
字段的可能值是什么。 What checks on this value should I be making to only delete the permanently invalid tokens?我应该对该值进行哪些检查以仅删除永久无效的令牌?
sender.send(gcmMessage, { registrationTokens: fcmTokens }, (error, response) => {
if (error) {
console.log(error);
} else {
const failedTokens = fcmTokens.filter((_, index) => response.results[index].error != null);
if (failedTokens.length) {
// If there are failed tokens, check these to know whether we should delete them.
this.clearUserFcmTokens(userID, failedTokens);
}
}
});
The two most common error codes that indicate that the token should be removed are messaging/invalid-registration-token
and messaging/registration-token-not-registered
.指示应删除令牌的两个最常见的错误代码是messaging/invalid-registration-token
和messaging/registration-token-not-registered
。
I recommend basing your logic on this code in the functions-samples repo .我建议您的逻辑基于functions-samples repo中的这段代码。
I got an answer from the repo itself.我从回购协议本身得到了答案。
They recommend checking if the error matches 'NotRegistered'
.他们建议检查错误是否匹配'NotRegistered'
。
I've modified the code to check 'NotRegistered'
and 'InvalidRegistration'
:我修改了代码以检查'NotRegistered'
和'InvalidRegistration'
:
sender.send(gcmMessage, { registrationTokens: fcmTokens }, (error, response) => {
if (error) {
console.log(error);
} else {
const invalidTokenErrorKeys = ['NotRegistered', 'InvalidRegistration'];
const failedTokens = fcmTokens.filter((_, index) => response.results[index].error != null && invalidTokenErrorKeys.includes(response.results[index].error));
if (failedTokens.length) {
this.clearUserFcmTokens(userID, failedTokens);
}
}
});
The values of the error field match the HTTP Code column from this link:错误字段的值与此链接中的 HTTP 代码列匹配:
https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
examples: InvalidRegistration
, NotRegistered
, MessageTooBig
, Unavailable
etc.示例: InvalidRegistration
、 NotRegistered
、 MessageTooBig
、 Unavailable
等。
Issue and response on the repo https://github.com/ToothlessGear/node-gcm/issues/360 repo 上的问题和响应https://github.com/ToothlessGear/node-gcm/issues/360
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.