2012-06-08 58 views
1

我的PHP代碼有點問題。 我沒有得到它的工作,我發送的消息,將在所有設備女巫保存在我的數據庫收到。推送通知不在多個設備上接收 - 只能在一臺設備上推送

設備上的推送應用程序自動在我的MySQL數據庫中註冊deviceToken。這一切工作正常。

最大的問題是,我只從數據庫中獲取推送消息給FIRST設備。其他設備不會收到任何消息。

這裏我的代碼:

<?php 

require_once('konfiguration.php'); 

$sql = "SELECT deviceToken FROM DeviceTokens"; 
$dbquery = mysql_query($sql); 

// check checkboxes 
$message = $_POST['alert']; 
$badge = $_POST['badge']; 
$sound = $_POST['sound']; 
// Construct the notification payload 
$body = array(); 
// anti-error variable 
$nothing = true; 
// get user input 
if ($message) { $alert=$_POST['message']; $nothing = false; 
$body['aps']['alert'] = $alert; } 
if ($badge) { $nothing = false; 
if($_POST['number']) $number=(int)$_POST['number']; 
else $number=1; 
$body['aps']['badge'] = $number; } 
if ($sound) { $nothing = false; 
$body['aps']['sound'] = 'beep.wav'; } 
// if input not correct, scream 
if ($nothing || ($message && $alert=="")) { echo "Falscher Input!"; exit(); } 
//make new stream context 
$ctx = stream_context_create(); 
// set parameters 
$streamContext = stream_context_create(); 
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'w3workProd.pem'); 
//stream_context_set_option($streamContext, 'ssl', 'passphrase', 'nils'); 

$socketClient = stream_socket_client('ssl://gateway.push.apple.com:2195', $error,  
$errorString, 60, STREAM_CLIENT_CONNECT, $streamContext); 

$payload = json_encode($body); 

print "sending message :" . $payload . "\n"; 

while($row = mysql_fetch_array($dbquery)) { 
$message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) 
.chr(0) . chr(strlen($payload)) . $payload; 
$deviceToken = str_replace(' ', '', $row['deviceToken']); 
$message = pack('CnH*', 0, 32, $deviceToken); 
$message = $message . pack('n', strlen($payload)); 
$message = $message . $payload; 

fwrite($socketClient, $message); 
echo ($row['deviceToken']); 
} 

fclose($socketClient); 
exit(); 
?> 

將是很好,如果你可以提供幫助。 爲什麼一個設備的工作......而不是與其他人...

+0

只有接收信息的設備才能發送信息嗎? – Leri

+0

只有設備獲得推,deviceToken是數據庫中的第一個(取決於ID號)。其他人沒有得到任何消息。 我通過html網站的一個小接口發送消息。所有工作都很好,但只有第一個令牌用於消息。其他人沒有收到任何東西 – emitremmus

+0

刪除所有'出口'然後嘗試。也許其中一個退出腳本(如果在循環中使用)。 – Leri

回答

0

你試過

STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT

相關問題