APNS:无效令牌导致所有后续推送通知失败 [英] APNS: Invalid token causes all subsequent push notifications to fail

查看:18
本文介绍了APNS:无效令牌导致所有后续推送通知失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sly PHP 推送通知库,但是无论我发送所有我的推送通知完全是手动的,没有库(就像下面的脚本一样).如果我的 SQL 数据库中存在无效令牌并且我尝试向该令牌发送推送,我将与 ssl://gateway.push.apple.com:2195 和所有后续令牌断开连接不接收推送.我怎样才能发现无效令牌并将它们从我的数据库中删除,或者在遇到无效令牌后继续发送推送?我在下面的 php 脚本(虽然不是上面的库)遇到了同样的错误:

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). 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);

推荐答案

此问题已解决 此处.我所做的是按 ID 对令牌进行排序,当令牌无效时,我将其从我的 SQL 数据库中删除,并继续向那些 ID 高于无效令牌的令牌发送推送(因此按 ID 排序至关重要).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆