简体   繁体   English

查找适用于iOS和Android的无效推送令牌

[英]Find invalid push tokens for ios and android

I am developing a mobile application having push notification feature [Android and iOS].I am using node-gcm and node-apn for sending push. 我正在开发具有推送通知功能的移动应用程序[Android和iOS]。我正在使用node-gcmnode-apn发送推送。

Is there any way to find tokens are invalid or not (iOS/Android registration token) ,so that I can remove them from my database? 有什么办法可以找到令牌是否无效(iOS / Android注册令牌),以便可以从数据库中删除它们?

This is how I solved it in my project: 这是我在项目中解决的方法:

[Android] [Android]

If you pass array of tokens to node-gcm in response you'll get an array with length equals tokens count. 如果将令牌数组传递给node-gcm作为响应,您将得到一个长度等于令牌计数的数组。 That array contains response for each token - success or error. 该数组包含每个令牌的响应-成功或错误。 Based on error you can decide whether to delete token or not: 根据错误,您可以决定是否删除令牌:

// 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]

I have something similar for iOS. 对于iOS,我也有类似的东西。 But worth noting that we use HTTP2 APN. 但值得注意的是,我们使用HTTP2 APN。 So below solution will work for you only if you use HTTP2 for APN too: 因此,只有将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}`);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM