推送消息能不能区分禁止通知和卸载两种类型?

巴扎黑
发布: 2016-11-21 09:20:16
原创
1230人浏览过

消息推送ios用了apns,android用的是gcm。推送失败都会返回无效的token,但是无效的tokne中,能不能区分到哪些是禁止通知,哪些是卸载app导致的呢? 

1 apns php 的推送返回错误处理 
push.php 

if (!empty($aMessage['ERRORS'])) {
foreach($aMessage['ERRORS'] as $aError) {
if ($aError['statusCode'] == 0) {
$this->_log("INFO: Message ID {$k} {$sCustomIdentifier} has no error ({$aError['statusCode']}), removing from queue...");
$this->_removeMessageFromQueue($k);
continue 2;
} else if ($aError['statusCode'] > 1 && $aError['statusCode'] <= 8) {
$this->_log("WARNING: Message ID {$k} {$sCustomIdentifier} has an unrecoverable error ({$aError['statusCode']}), removing from queue without retrying...");
$this->_removeMessageFromQueue($k, true);
continue 2;
}
}
if (($nErrors = count($aMessage['ERRORS'])) >= $this->_nSendRetryTimes) {
$this->_log(
"WARNING: Message ID {$k} {$sCustomIdentifier} has {$nErrors} errors, removing from queue..."
);
$this->_removeMessageFromQueue($k, true);
continue;
}
}
登录后复制

通过禁止通知,apns不会报错,不会将这个token当成无效或错误的token。 

卸载app,会调用到以下判断,statusCode等于8 

 if ($aError['statusCode'] > 1 && $aError['statusCode'] <= 8) {
$this->_log("WARNING: Message ID {$k} {$sCustomIdentifier} has an unrecoverable error ({$aError['statusCode']}), removing from queue without retrying...");
$this->_removeMessageFromQueue($k, true);
continue 2;
}
登录后复制

因此,apns应该是可以区分卸载导致的推送失败,但是禁止通知则无法反应 

2 GCM的错误判断代码分析: 
Response.class.php 

/**
     * Returns an array containing invalid registration ids
     * They must be removed from DB because the application was uninstalled from the device.
     *
     * @return array
     */
    public function getInvalidRegistrationIds()
    {
        if ($this->getFailureCount() == 0) {
            return array();
        }
        $filteredResults = array_filter($this->results,
            function($result) {
                return (isset($result['error']) 
                && (($result['error'] == "NotRegistered")  || ($result['error'] == "InvalidRegistration")));
            });
        return array_keys($filteredResults);
    }
    /**
     * Returns an array of registration ids for which you must resend a message (?),
     * cause devices aren't available now.
     *
     * @TODO: check if it be auto sended later
     *
     * @return array
     */
    public function getUnavailableRegistrationIds()
    {
        if ($this->getFailureCount() == 0) {
            return array();
        }
        $filteredResults = array_filter($this->results,
            function($result) {
                return (
                    isset($result['error'])
                    &&
                    ($result['error'] == "Unavailable")
                    );
            });
        return array_keys($filteredResults);
    }
登录后复制

如果禁止通知,上述2个方法都不会写入错误token,也就是说禁止通知,token也是有效的,且不会返回错误。 
如果是卸载app,则会执行到getInvalidRegistrationIds,且$result['error']==NotRegistered 

这样,GCM如果返回的是NotRegistered,则说明是卸载产生的错误信息,而禁止通知,GCM是当成正常token发出去的。 


通过以上测试,说明apns和gcm对禁止通知都是当成正常token来处理的,而卸载app则会当成无效的token。(卸载后重装的话,会生成新的token)

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号