【发布时间】:2017-05-11 07:15:46
【问题描述】:
我正在开发一个具有推送通知功能的移动应用程序 [Android 和 iOS]。我正在使用 node-gcm 和 node-apn 发送推送。
有什么方法可以找到令牌是否无效(iOS/Android 注册令牌),以便我可以将它们从我的数据库中删除?
【问题讨论】:
标签: android ios node.js express push-notification
我正在开发一个具有推送通知功能的移动应用程序 [Android 和 iOS]。我正在使用 node-gcm 和 node-apn 发送推送。
有什么方法可以找到令牌是否无效(iOS/Android 注册令牌),以便我可以将它们从我的数据库中删除?
【问题讨论】:
标签: android ios node.js express push-notification
这就是我在项目中解决它的方法:
[安卓]
如果您将令牌数组传递给 node-gcm 作为响应,您将得到一个长度等于令牌计数的数组。该数组包含每个令牌的响应 - 成功或错误。您可以根据错误决定是否删除令牌:
// This is response from Google
response.results.map((item,index) => {
if (item.error) {
// If Google doesn't recognize token I don't need to keep it anymore
if (item.error === 'NotRegistered') {
failedTokens.push(androidTokens[index]);
} else {
logger.error(`Push notification was not sent because: ${item.error}`);
}
}
});
failedTokens.map(token => {
this.deleteDeviceToken('android', appName, token);
});
[iOS]
我在 iOS 上也有类似的东西。但值得注意的是,我们使用的是 HTTP2 APN。因此,仅当您也将 HTTP2 用于 APN 时,以下解决方案才适用于您:
// Response from Apple
response.failed.map(failure => {
if (failure.error) {
logger.error(`Error during sending notification: ${JSON.stringify(failure.error)}`);
} else {
// If APN returned HTTP 400 with status BadDeviceToken or HTTP 410 with status Unregistered
// then delete invalid tokens.
if (failure.response.reason === 'BadDeviceToken' || failure.response.reason === 'Unregistered') {
this.deleteDeviceToken('ios', appName, failure.device);
} else {
logger.error(`Push notification was not sent because: ${failure.response.reason}`);
}
}
});
【讨论】: