APNS:无效令牌会导致所有后续推送通知失败

时间:2016-09-28 23:17:28

标签: php ios apple-push-notifications apns-php

我正在使用Sly PHP Push Notification library,但是同样的事情发生在我是否完全手动发送所有推送通知,没有库(如下面的脚本中)。如果我的SQL数据库中存在无效令牌,并且我尝试向该令牌发送推送,则我与ssl://gateway.push.apple.com:2195断开连接,并且所有后续令牌都不会收到推送。如何找到无效令牌并将其从我的数据库中删除,或者在遇到无效令牌后继续发送推送?我在下面的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);

1 个答案:

答案 0 :(得分:0)

这已解决here。我所做的是通过ID对令牌进行排序,当令牌无效时,我将其从我的SQL数据库中删除,并继续向那些ID高于无效令牌的令牌发送(因此按ID排序至关重要)。 / p>