我有一个带有用于Apple推送通知的MySQL数据库的PHP脚本。该脚本有效,但并非所有通知都会收到通知。怎么回事?
<?PHP
$db_user = "xx_fw"; // Gebruiker voor MySQL
$db_pass = "xxx"; // Wachtwoord voor MySQL
$db_host = "localhost"; // Host voor MySQL; standaard localhost
$db_db = "xx_fw"; // Database
// Als je al ergens anders een database connectie hebt gemaakt,
// maak dan van de volgende twee regels commentaar (# of // ervoor zetten)
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_db);
$query = mysql_query("SELECT * FROM iospush");
$deviceToken = ARRAY();
while ($row = mysql_fetch_array($query)) {
$deviceToken[] = $row["token"];
}
if($_POST['politie']){
$message = stripslashes($_POST['message']);
$politiemessage = "POLITIEBERICHT: ".$message;
$payload = '{
"aps" :
{ "alert" : { "body" : "'.$politiemessage.'" },
"badge" : 1,
"sound" : "politiepush.wav"
}
}';
$ssl='ios-ssl.pem';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $ssl);
stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxx');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
print "Failed to connect $err $errstrn";
return;
} else {
print "Notifications sent!";
}
$devArray = array();
$devArray[] = $deviceToken;
foreach($deviceToken as $token){
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack ("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "n";
fwrite($fp, $msg);
echo "<br><br>";
echo "Verstuurd aan: ".$token;
echo "<br><br>";
}
fclose($fp);
} else if($_POST['message']){
$message = stripslashes($_POST['message']);
$payload = '{
"aps" :
{ "alert" : { "body" : "'.$message.'" },
"badge" : 1,
"sound" : "chime"
}
}';
$ssl='ios-ssl.pem';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $ssl);
stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxx');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
print "Failed to connect $err $errstrn";
return;
} else {
print "Notifications sent!";
}
$devArray = array();
$devArray[] = $deviceToken;
foreach($deviceToken as $token){
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack ("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "n";
fwrite($fp, $msg);
echo "<br><br>";
echo "Verstuurd aan: ".$token;
echo "<br><br>";
}
fclose($fp);
}
?>
问题是发送推送的方式,还是苹果方面出错了? 谢谢!
答案 0 :(得分:0)
尝试以下代码:
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
$a = stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__).'\push.pem');
// assume the private key passphase was removed.
// stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
//$status = array();
$status = '';
if ($fp) {
$status = "Notification sent successfully."; //$message;
} else {
$status = "Failed to sent notification.";
//
}
$payload = json_encode($body);
$msg = @(chr(0) . pack("n",32) . pack('H*', str_replace(' ','' , $deviceToken)) . pack("n",strlen($payload)) . $payload);
if($msg)
{
fwrite($fp, $msg);
if (!extension_loaded('openssl')) {
}
}
echo $status;
?>
注意#请使用正确的.pem文件for ios