2012-09-06 37 views
0

我必須做的有節省分貝,我有使PHP許多設備發送推送通知(分配)代碼:推送通知,如果deviceToken是錯的不是發送給其他

// Put your alert message here: 
$message = "send push"; 
// Put your private key's passphrase here: 
$passphrase = 'phrase'; 
$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns.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'. "<br>" ; 
ob_flush(); 
flush(); 

// Create the payload body 
$body['aps'] = array(
'alert' =&gt; $message, 
'sound' =&gt; 'default' 
); 

// Encode the payload as JSON 
$payload = json_encode($body); 
if (mysql_num_rows($results)!=0) { 
    while($row = mysql_fetch_array($results)) 
    { 
     $deviceToken= $row['deviceToken']; 

     if(isset($deviceToken)&&(strcmp($deviceToken,"(null)")!=0)){ 
      echo "<br>".$deviceToken."<br>"; 
      // Build the binary notification 
      $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
      $result = fwrite($fp, $msg, strlen($msg)); 
      if (!$result) 
       echo 'Message not delivered'; 
      else 
       echo 'Message successfully delivered' ; 
      ob_flush(); 
      flush(); 
     } 
    } 
    // Close the connection to the server 
    fclose($fp); 
} 

如果我有數據庫,只有deviceToken正確或值(空),消息發送,但如果我有一個deviceToken錯誤,我收到消息'消息已成功發送'已發送所有消息,但不工作。我犯了什麼錯誤?謝謝。

回答

0

你沒有錯,這是一個蘋果質量控制。我遇到了這個現象, - 如果我記得很清楚 - 如果設備令牌的結構是無效的,沒有推送通知被送到...

所以,如果你有一個有效的令牌,目前不屬於設備(例如,該設備已刪除您的應用程序),您的其他推送通知將被傳遞,但如果令牌似乎具有絕對錯誤的結構,則不會傳遞推送通知。

因此,這種現象對您的生產系統不會造成危險,因爲您的設備會向您的提供商的數據庫註冊一個有效的令牌。

也許更多的細節是本教程的註釋之間:http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

0

這不能做。如果推送通知是否成功發送,沒有確定的消息方式。還有其他的選擇,但它們並不是即時的,也不是可靠的。

  1. 確保應用程序安裝在用戶設備上。
  2. 當收到推送通知時,將邏輯添加到您的應用程序以發佈到您的數據庫。這樣你就知道推動是好的。這樣做的缺點是推送通知並不總是即時的,因此可能會有延遲。

用戶解釋這一點,並給予在另一篇文章的鏈接。 How to find the Apple Push Notification Delivered to user or not from our server?