Analyze php to do push server to realize ios message push

  • 2020-07-21 07:03:48
  • OfStack

The preparatory work
1. Get deviceToken for mobile phone registration application (iphone returns only 1 value deviceToken for mobile phone registration application)
2. Obtain ck. pem file (make the phone terminal)
3. Get pass phrase (for the mobile terminal)

testpush php file

<?php
// The phone registration app returns only 1 the deviceToken
$deviceToken = '6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 33bb9ef4 38a8aef9';
//ck.pem Customs code 
$pass = 'jetson';   
// The 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 it's the sound of push notifications on your phone) 
$sound = 'Duck.wav';
// The construction of the notification payload (i.e., the notification contains 1 Some information) 
$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),"\r\n";
// This is the dead end, 1 Generally, no modification is required. 
// only 1 The changes are: ssl://gateway.sandbox.push.apple.com:2195 This is the sandbox test address, ssl://gateway.push.apple.com:2195 Official 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 $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);
?>

Related articles: