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

巴扎黑
Lepaskan: 2016-11-22 14:35:20
asal
913 orang telah melayarinya

消息推送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[&#39;statusCode&#39;] > 1 && $aError[&#39;statusCode&#39;] <= 8) {
$this->_log("WARNING: Message ID {$k} {$sCustomIdentifier} has an unrecoverable error ({$aError[&#39;statusCode&#39;]}), removing from queue without retrying...");
$this->_removeMessageFromQueue($k, true);
continue 2;
}
}
if (($nErrors = count($aMessage[&#39;ERRORS&#39;])) >= $this->_nSendRetryTimes) {
$this->_log(
"WARNING: Message ID {$k} {$sCustomIdentifier} has {$nErrors} errors, removing from queue..."
);
$this->_removeMessageFromQueue($k, true);
continue;
}
}
Salin selepas log masuk

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

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

 if ($aError[&#39;statusCode&#39;] > 1 && $aError[&#39;statusCode&#39;] <= 8) {
$this->_log("WARNING: Message ID {$k} {$sCustomIdentifier} has an unrecoverable error ({$aError[&#39;statusCode&#39;]}), removing from queue without retrying...");
$this->_removeMessageFromQueue($k, true);
continue 2;
}
Salin selepas log masuk

因此,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[&#39;error&#39;]) 
                && (($result[&#39;error&#39;] == "NotRegistered")  || ($result[&#39;error&#39;] == "InvalidRegistration")));
            });
        return array_keys($filteredResults);
    }
    /**
     * Returns an array of registration ids for which you must resend a message (?),
     * cause devices aren&#39;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[&#39;error&#39;])
                    &&
                    ($result[&#39;error&#39;] == "Unavailable")
                    );
            });
        return array_keys($filteredResults);
    }
Salin selepas log masuk

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

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


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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!