Home  >  Article  >  Backend Development  >  Parse PHP as a push server to implement ios message push_PHP tutorial

Parse PHP as a push server to implement ios message push_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:01:49785browse

Preparation work
1. Get the deviceToken of the mobile phone registration application (the unique value deviceToken is returned when the iphone phone registers the application)
2. Get the ck.pem file (for the mobile phone) Give)
3. Get the pass phrase (give it on the mobile phone)

testpush.php file

Copy code The code is as follows:

//The mobile phone registration application returns the unique deviceToken
$deviceToken = '6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 33bb9ef4 38a 8aef9';
//ck.pem pass password
$pass = 'jetson';
//Message content
$message = 'A test message!';
//I don’t know what the badge is either
$badge = 4;
//I don’t know what the sound is (maybe it’s the sound when a message is pushed to the phone)
$sound = 'Duck.wav';
//The construction notification is valid Payload (i.e. 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 json data
$payload = json_encode($body);
echo strlen($payload),"rn";
//The writing method below is dead writing, and generally does not need to be modified.
//The only thing that needs to be modified is: ssl://gateway.sandbox.push.apple.com:2195 This is the sandbox test address. ssl://gateway.push.apple.com:2195 Official release 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 , 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
}
else {
print "Connection OKn
";
}
// 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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327978.htmlTechArticlePreparation 1. Get the deviceToken of the mobile phone registration application (the unique value deviceToken is returned when the iPhone phone registers the application) 2. Get ck.pem file (for mobile phones) 3. Get the pass phrase (for...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn