使用下面
<?php
// Put your device token here (without spaces):
$deviceToken = '123456789';
//
// Put your private key's passphrase here:
$passphrase = 'ProjectName';
// Put your alert message here:
$message = 'My first push notification!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
// 'ssl://gateway.push.apple.com:2195', $err,
'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(
'content-available'=> 1,
'alert' => $message,
'sound' => 'default',
'badge' => 0,
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
使用此simplepush.php文件命令創建PEM文件,並用它在上面的代碼
$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem
# Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert.
$ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12
Enter Import Password:
MAC verified OK
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
# To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings.
$ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem
Enter pass phrase for PushChatKey1.pem:
writing RSA key
# To join the two .pem file into one file:
$ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem
之後去simplepush.php位置和fire命令 - > php simplepush.php
This您可以測試推送套件通知設置體系結構的方式。
此設置和設置將幫助您瞭解證書是否已正確創建和使用。如果證書創建和使用中存在問題,那麼它將顯示發送通知,但您絕不會在設備上收到通知。
在任何php或.net或其他代碼將永遠不會確認通知是在隊列中或它已被取消,它會始終顯示您通知發送。
它的那樣簡單的: { APS = { 警報= { 消息= { MSG = 「SOCKET_DISCONNECT」; }; }; badge = 0; sound =「ping.aiff」; }; } –
延遲必須在蘋果服務器或與手機(或手機)的連接上...您是否嘗試重新啓動手機並確保它具有強大的無線連接?也許你的網絡提供商造成了問題。 – ostergaard
您是否設置了優先級? https://github.com/node-apn/node-apn/blob/master/doc/notification.markdown#notificationpriority – ostergaard