[英]APNS: Invalid token causes all subsequent push notifications to fail
I'm using the Sly PHP Push Notification library , but the same thing happens whether I send all my push notifications completely manually, without the library (like in the script below). 我正在使用Sly PHP推送通知库 ,但是无论我是否完全手动发送所有推送通知而没有该库,都会发生同样的事情(如下面的脚本中所示)。 If there is an invalid token in my SQL database and I attempt to send a push to that token, I am disconnected from ssl://gateway.push.apple.com:2195
and all the subsequent tokens do not receive the push. 如果我的SQL数据库中存在无效的令牌,并且我尝试向该令牌发送推送,则我与ssl://gateway.push.apple.com:2195
断开连接,并且所有后续令牌均未收到该推送。 How can I either spot invalid tokens and remove them from my database, or continue to send out pushes after I come across an invalid token? 如何发现无效令牌并将其从数据库中删除,或者在遇到无效令牌后如何继续发送推送? A php script I have below (though not the Library above) suffers from the same bug: 我下面的一个php脚本(尽管不是上面的库)遭受了相同的错误:
// Put your device token here (without spaces):
$iosTokens = array(xxxx, xxxx, xxxx);
// Put your private key's passphrase here:
$passphrase = '';
$message = "Message";
$url = "URL";
if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '../../../PEMs/siouxFallsStampede.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'link_url' => $url,
);
// Encode the payload as JSON
$payload = json_encode($body);
foreach($iosTokens as $devicetoken) {
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $devicetoken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
var_dump($result);
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);
This was solved here . 这在这里解决了 。 What I did was ordered the tokens by ID, when a token was invalid, I removed it from my SQL database and continued sending a push to just those tokens with an ID higher than the invalid one (so ordering by ID is crucial). 我所做的工作是按ID对令牌进行排序,当令牌无效时,我将其从SQL数据库中删除,并继续仅向ID高于无效令牌的那些令牌发送推送(因此按ID排序至关重要)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.