欢迎所有程序员。 我得到一个系统(php / MySQL)进行一些更改。我找到的问题之一是Ios推送通知。代码在这里
function send_notification_ios($cert,$passphrase) {
var $error;
var $errorString;
$keyPath = dirname(__FILE__) . '/' .$cert.'.pem';
$keyPassword = $passphrase;
$gateway = 'gateway.push.apple.com:2195';
$context = stream_context_create();
if (!stream_context_set_option($context, 'ssl', 'local_cert', $certificate)){
return json_encode(array('success' => 0, 'failure' => 1, 'reason'=>'stream_context local_cert'));
}
if(!stream_context_set_option($context, 'ssl', 'passphrase', $passphrase)){
return json_encode(array('success' => 0, 'failure' => 1, 'reason'=>'stream_context passphrase'));
}
$socket = stream_socket_client($gateway, $error, $errorString, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $context);
if (!$socket) {
return json_encode(array('success' => 0, 'failure' => 1, 'reason'=>'soket: '.$error.' '.$errorString));
}
$payload = json_encode([
'aps' => [
'alert' => 'Hello World',
'sound' => 'default',
'badge' => 1
]
]);
$payPack = chr(2).pack('n', strlen($payload)).$payload;
$expDate = chr(3).pack('n', 4).pack('N', time() + 86400);
$priority = chr(4).pack('n', 1).chr(10);
$inner =
chr(1).pack('n', 32).pack('H*', $apikey)
.$payPack
.$expDate
.$priority;
$notification =
chr(2)
.pack('N',strlen($inner))
.$inner;
stream_set_blocking($socket, 0);
$socketResult = fwrite($socket,$notification,strlen($notification));
if($socketResult){
$success++;
}
else{
$failure++;
}
fclose($socket);
return json_encode(array('success' => $success, 'failure' => $failure);
}
此功能的结果是{success:1,failure:0)。但ios应用程序没有收到通知。 (例如android app收到android通知,从这个系统发送没有任何问题)。 所有建议将不胜感激。