PHP中的多个通知推送iPhone
Posted
技术标签:
【中文标题】PHP中的多个通知推送iPhone【英文标题】:Multiple notification push iPhone in PHP 【发布时间】:2013-03-07 09:48:24 【问题描述】:我的代码有点问题
<?php
define("DB_HOST","");
define("DB_USER","");
define("DB_PASSWORD","");
define("DB_DATABASE","");
$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("erreur connection");
mysql_select_db(DB_DATABASE);
//Make $i Global and set it to zero
global $i;
$i=0;
//Make $j Global and set it to zero
global $j;
$j=0;
//Makes $message global and defines the text for the message
global $message;
$message = "Hello everybody";
//Gets the device token
$result = mysql_query("select token FROM users_iphone");
while($row = mysql_fetch_array($result))
//Gets the message string
GLOBAL $message;
//Gets the devicetoken from the database and sets a string with the token
$devicetoken = $row['deviceToken'];
//Calls the function to send the message and defines parameter for the function. In this case the device token and the message to be sent.
sendPOST($devicetoken, $message);
//Gets $i, defined earlier and adds 1 to it for each device token in our database. We are counting how many devicetokens we have.
global $i;
$i++;
//Function that sends our push notification
function sendPOST ($deviceToken, $messageGlobal)
$deviceToken = '<5398e918 2c303556 23dfff5e 2754ff54 e55106f9 8d07ea4b 96cd88ba 288f526f>';
$deviceToken = str_replace('<', '', $deviceToken);
$deviceToken = str_replace('>', '', $deviceToken);
$deviceToken = str_replace(' ', '', $deviceToken);
//token is good with no space and no > and <
$passphrase = 'mypassphrase';
//start of apns code
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$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);
$body['aps'] = array(
'alert' => $messageGlobal,
'sound' => 'default'
);
$payload = json_encode($body);
$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' . PHP_EOL;
//end of apns code
//gets $j and adds 1
global $j;
$j++;
fclose($fp);
//checks if $j is equal to $i. If so it means all the messages were delivered to apns (This doesn't mean that all the messages will be delivered by apple. It does mean that they were delivered to apple from us.
if ($j==$i)
echo "<h1><b>Message's Successfully delivered.</b></h1><br>";
echo "<b>Message:</b> $message<br>";
echo "<b>Messages Delivered: </b>$j out of $i";
//this checks to see if $i is greater that $j. If not then that means something went wrong on our end. Therefore we have not delivered all the messages from our end to apple successfully. This could mean a bad connection or server failure/error.
elseif ($i>$j)
echo "<h1><b>Not all messages have been delivered (at least on our end). $j out of $i messages were delivered on our end.</b></h1>";
echo "<b>Message:</b> $message";
echo "<b>Messages Delivered: </b>$j out of $i";
?>
当我在我的 php 文件中直接记下 $deviceToken 时,会收到通知,但是当我想在我的数据库中恢复 deviceToken 时它不起作用!
请帮帮我,我的代码有问题?
非常感谢
【问题讨论】:
这需要首先进行基本调试。你在什么时候丢失了令牌?究竟会发生什么?您收到了哪些错误消息? 第一,你可以向单个设备发送通知吗? 谢谢,我没有错误消息,只是我没有收到通知,thzt 可以从我的数据库中的恢复 deviceToken 派生,因为如果我在函数 sendPOST 中写入,$deviceToken = '';它正在工作 是的,当我发送一个通知时它正在工作 【参考方案1】: 根据我的经验,如果您正确完成了所有编码和其他工作,但是,在您的数据库中有 invalid(在测试期间插入)令牌,并在循环中向所有设备发送推送消息,推送在第一个错误/无效令牌之后不会发送通知,即使您还有更多好的令牌。所以,请检查一下。 APNS-php is an advanced API 帮助您进行多次发送、检测错误令牌等。好吧,当然您可以不使用它,但是测试 Pushnotification 发送需要几乎 90% 的开发时间,而这个 API 和它很酷的异常处理可以震撼您的世界!【讨论】:
对不起,我不明白... :s 哪一部分?这与您的要求无关吗?对我来说,很难查看整个代码并遵循。以上是关于PHP中的多个通知推送iPhone的主要内容,如果未能解决你的问题,请参考以下文章