2016-09-28 165 views
0

我使用的是Sly PHP Push Notification library,但是無論我是否完全手動發送所有推送通知(沒有庫(如下面的腳本)),都會發生同樣的情況。如果我的SQL數據庫中存在無效令牌,並且我嘗試向該令牌發送推送,則我從ssl://gateway.push.apple.com:2195斷開連接,並且所有後續令牌都不會收到推送。我怎麼能發現無效令牌並將其從我的數據庫中刪除,或者在遇到無效令牌後繼續發送推送消息? PHP腳本我有以下(雖然不是上面的庫)從相同的錯誤遭受:APNS:無效令牌導致所有後續推送通知失敗

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

回答

0

這是解決here。我所做的是通過ID命令令牌,當令牌無效時,我將其從我的SQL數據庫中刪除,並繼續只發送一個ID高於無效ID的令牌(因此按ID排序非常重要)。