This article is the PHP to do push service End-to-end implementation of the iOS message push method of a detailed analysis of the introduction, the need for friends under the reference
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 a unique Devicetoken
$deviceToken = ' 6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 3 3bb9ef4 38a8aef9 ';
//ck.pem Customs Password
$pass = ' Jetson ';
//message content
$message = ' A test message! ';
//badge I don't know what
$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 Notice 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;
//convert array data to JS On data
$payload = Json_encode ($body);
Echo strlen ($payload), "RN";
///Below is written in dead writing, generally do not need to modify,
///The only change 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 $errstrn ';
return;
}
Else {
print "Connection okn<br/>";
} The
//Send message
$msg = chr (0). Pack ("n",). Pack (' h* ', Str_replace (', ', ', $deviceToken)). Pack ("n", strlen ($ Payload)). $payload;
Print "Sending message:". $payload. "N";
Fwrite ($fp, $msg);
Fclose ($FP);
?