APNS:无效令牌导致所有后续推送通知失败 [英] APNS: Invalid token causes all subsequent push notifications to fail
问题描述
我正在使用狡猾的PHP推送通知库,但是无论我是否发送全部,都会发生相同的情况我的推送通知完全是手动的,没有库(如下面的脚本中所示).如果我的SQL数据库中存在无效的令牌,并且我尝试向该令牌发送推送,则我与 ssl://gateway.push.apple.com:2195
和所有后续令牌断开了连接不接受推送.如何发现无效令牌并将其从数据库中删除,或者遇到无效令牌后如何继续发送推送?我下面的一个php脚本(虽然不是上面的库)却遭受相同的错误:
///将您的设备令牌放在此处(不带空格):$ iosTokens =数组(xxxx,xxxx,xxxx);//将私钥的密码放在这里:$ passphrase ='';$ message =消息";$ url ="URL";如果(!$ message ||!$ url)exit('示例用法:$ 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);//打开与APNS服务器的连接$ fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$ err,$ errstr,60,STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT,$ ctx);如果(!$ fp)exit(无法连接:$ err $ errstr".PHP_EOL);回显已连接到APNS".PHP_EOL;//创建有效载荷主体$ body ['aps'] = array('alert'=>$消息,'声音'=>'默认','link_url'=>$ url,);//将有效负载编码为JSON$ payload = json_encode($ body);foreach($ iosTokens as $ devicetoken){//构建二进制通知$ msg = chr(0).pack('n',32).pack('H *',$ devicetoken).pack('n',strlen($ payload)).$ payload;//将其发送到服务器$ result = fwrite($ fp,$ msg,strlen($ msg));var_dump($ result);如果(!$ result)回显消息未传递".PHP_EOL;别的回显消息已成功传递".PHP_EOL;}//关闭与服务器的连接fclose($ fp);
此问题已解决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). 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. 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:
// 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).
这篇关于APNS:无效令牌导致所有后续推送通知失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!