簡體   English   中英

APNS:無效的令牌導致所有后續的推送通知失敗

[英]APNS: Invalid token causes all subsequent push notifications to fail

我正在使用Sly PHP推送通知庫 ,但是無論我是否完全手動發送所有推送通知而沒有該庫,都會發生同樣的事情(如下面的腳本中所示)。 如果我的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);

這在這里解決 我所做的工作是按ID對令牌進行排序,當令牌無效時,我將其從SQL數據庫中刪除,並繼續僅向ID高於無效令牌的那些令牌發送推送(因此按ID排序至關重要)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM