2013-04-17 164 views
0

我有以下php代码为ios推送notification.Here我代码为2个设备使用循环在fwrite()部分。当前的代码工作正常。我的疑问是,我可以直接传递设备令牌数组而不使用for循环吗?ios推送通知多个设备

<?php 
// Put your device token here (without spaces): 
$deviceToken[0] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

$deviceToken[1] = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'; 

// Put your private key's passphrase here: 
$passphrase = '123456'; 

// Put your alert message here: 
$message = 'multiple device push notification...!'; 

//////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'abc.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
'ssl://gateway.sandbox.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', 
'badge' => '+1' 
); 

// Encode the payload as JSON 
$payload = json_encode($body); 
for($i=0;$i<2;$i++) 
{ 
// Build the binary notification 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken[$i]) . pack('n', strlen($payload)) .  $payload; 

// Send it to the server 
$result = fwrite($fp, $msg, strlen($msg)); 
echo "msg may be delivered"; 
} 

if (!$result) 
echo 'Message not delivered' . PHP_EOL; 
else 
echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 
+0

可能重复的[iOS推送通知问题](http://stackoverflow.com/questions/4504448/ios-push-notifications-question) – DjangoDev

回答

6

它的design by default,但没有选择传递设备令牌数组。你必须遍历循环。

+0

是否有任何替代选项可用,我可以创建一个device_tokens数组上传上传与苹果有效载荷和通知去所有设备中指定的设备可以说,例如。 1000个用户或更多?请帮忙 –

+2

它的设计默认情况下不能发送数组,你必须遍历所有设备令牌的数组发送给多个推送通知。 –

+0

我的代码与上面几乎相同,但是当我尝试发送推送到多个设备时(fwrite到每个'device_id'),它的工作原理是,如果我有一个包含2-3个设备ID的数组(仅测试那么多),但当我拥有〜300个设备ID时不起作用 – Darpan

0

替代此方法将使用第三方,如amazon SNS service。在这里,您可以发布到主题(一个请求),并且订阅此主题的所有设备都将收到通知。

相关问题