Preparatory work
1. Access to mobile phone registration application Devicetoken (iphone registration when the application returns a unique value Devicetoken)
2. Get Ck.pem file (do phone-side to)
3. Get pass phrase (do phone-side)
testpush.php File
Copy Code code as follows:
<?php
Mobile registration Application returns the only Devicetoken
$deviceToken = ' 6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 33bb9ef4 ';
CK.PEM Customs Password
$pass = ' Jetson ';
Message content
$message = ' A test message! ';
Badge, I don't know what it is.
$badge = 4;
Sound I don't know what it is (maybe the beep when I push the message to the phone)
$sound = ' duck.wav ';
Construction of the notification payload (that is, some information contained in the notification)
$body = Array ();
$body [' id '] = "4f94d38e7d9704f15c000055";
$body [' aps '] = Array (' alert ' => $message);
if ($badge)
$body [' APS '] [' badge '] = $badge;
if ($sound)
$body [' APS '] [' sound '] = $sound;
Converting array data to JSON data
$payload = Json_encode ($body);
echo strlen ($payload), "\ r \ n";
The writing below is dead writing, generally do not need to modify,
The only thing to be modified is: ssl://gateway.sandbox.push.apple.com:2195 This is the sandbox test address, ssl://gateway.push.apple.com:2195 officially released address
$ctx = Stream_context_create ();
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', ' Ck.pem ');
Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $pass);
$fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $errstr, Stream_client_connect, $ CTX);
if (! $fp) {
Print "Failed to connect $err $errstr \ n";
Return
}
else {
Print "Connection ok\n<br/>";
}
Send Message
$msg = Chr (0). Pack ("n", 32). Pack (' h* ', Str_replace (', ', ', $deviceToken)). Pack ("n", strlen ($payload)). $payload;
Print "Sending message:". $payload. "\ n";
Fwrite ($fp, $msg);
Fclose ($FP);
?>