android - 查找 ios 和 android 的无效推送 token

标签 android ios node.js express push-notification

我正在开发具有推送通知功能 [Android 和 iOS] 的移动应用程序。我正在使用 node-gcmnode-apn用于发送推送。

有什么方法可以找到 token 是否无效(iOS/Android 注册 token ),以便我可以将它们从我的数据库中删除?

最佳答案

这就是我在项目中解决这个问题的方法:

[Android]

如果您将 token 数组传递给 node-gcm 作为响应,您将获得一个长度等于 token 数的数组。该数组包含每个标记的响应 - 成功或错误。根据错误,您可以决定是否删除 token :

// 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}`);
        }
    }
});

关于android - 查找 ios 和 android 的无效推送 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908858/

相关文章:

java - Android - 服务应用程序如何将 KeyEvents 调度到另一个前台应用程序

ios - UIWebView 源代码

ios - 我无法通过 class_copyPropertyList 使用 swift 获取类的属性

node.js - 如何使用 Nodeclipse 和 Node Express 调试客户端 JavaScript 和服务器端

Android Studio 没有构建完整的文件

android - 来自服务的 overridePendingTransition 给出错误

java - Android:使用 setBackgroundDrawable 设置后位图变小

ios - FBSDK_CANOPENURL_FBAPI 未声明标识符

node.js - 无法读取未定义的属性 'title'。表达

javascript - Chrome 和 Firefox 中的 WebSocket 在一分钟不活动后断开连接