SoFunction
Updated on 2025-03-10

Analyze php to push the server to implement ios message push


<?php
//Register the application on your mobile phone and return the only deviceToken
$deviceToken = '6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 33bb9ef4 38a8aef9';
//Permission Password
$pass = 'jetson';  
//Message content
$message = 'A test message!';
//I don't know what badge is
$badge = 4;
//I don't know what sound is (maybe it's the prompt sound when pushing messages to the phone)
$sound = '';
//Constructed notification 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),"\r\n";
//The following is a dead-end writing method, generally no need to modify it.
//The only thing to be modified is: ssl://:2195 This is the sandbox test address, ssl://:2195 officially released address
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', ''); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://: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);
?>