Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quickly send an Apple Push Notification using PHP
<?php
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = 'apns-dev.pem';
$apnsPort = 2195;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'Oh hai!', 'badge' => 1, 'sound' => 'default');
$output = json_encode($payload);
$token = pack('H*', str_replace(' ', '', $token))
$apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
@samayananthan
Copy link

samayananthan commented Apr 13, 2015

this code i am using but not work please help me

function pushnotification()
{
echo "sucess";
$deviceToken='974eb97d262c6a74e22ee7a632b1444d109956f7e48b44255aff1681bbc1d741';
$message="moadmin";
$payload= '{
"apps" :
{
"alert":"'.$message.'"
"badge":1,
"sound":"bingbong.aiff"
}

        }';
   $ctx=stream_context_create();

// stream_context_set_option($ctx, 'ssl', 'local_cert', './certs/ck.pem');
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
//stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// $fb=stream_context_set_option($ctx,'ssl','passphrase','1234');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
// $fp=stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr,60,STREAM_CLIENT_CONNECT,$ctx);
if ($fb) {
print "Failed to connect $err $errstr";
return;
}
else
{
print "Notification sent";
}

   $devarray=array();
   $devarray=$deviceToken;
   foreach ($devarray as $deviceToken) {
    $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);

}
}

@KushThakkar
Copy link

KushThakkar commented Apr 4, 2016

$apnsCert = 'ck.pem';

change this line with below line:

$apnsCert = dirname(FILE).'/abc.pem';

@shramee
Copy link

shramee commented Jun 13, 2017

Will this work from localhost?

@andreszs
Copy link

andreszs commented Jul 21, 2017

This code is obsolete, for the Legacy APNS interface.

@shayo
Copy link

shayo commented Jul 25, 2017

@andreszs , is there a solution for non legacy interface?

@hfarhanahmed
Copy link

hfarhanahmed commented May 1, 2018

How can we test if the push is successfully sent, I am using this code, the code runs without any error but the push is not received on ios device.

@andreszs
Copy link

andreszs commented Jun 20, 2018

The previous code has serious flaws with the packaging of bytes, Try this instead:
http://www.kubilayerdogan.net/sample-apple-push-notification-php-script/

Or just update the existing sample with this code, which works just fine:

/* original code until line 8 goes here */

// Better payload with title and body; for silent push omit 'alert' and 'sound'
$payload['aps'] = array(
	'alert' => array(
		'title' => 'Sample push number ' . mt_rand(100, 999),
		'body' => 'Sample push body goes here'
	),
	'badge' => 9,
	'sound' => 'default',
	'my_value_1' => 'more data',
	'my_value_2' => 'even more data'
);

// JSON-encode payload
$output = json_encode($payload, JSON_NUMERIC_CHECK);

// Cancel if payload too long
if(strlen($output) > 2048){
	die('Payload too long ('.strlen($output).'): Aborted');
}

// Build the binary notification (correctly)
$token = pack('H*', str_replace(' ', '', $token)); /* str_replace useless since token does not appear to have spaces? */
$apnsMessage = chr(0).pack('n', 32).$token.pack('n', strlen($output)).$output;

// Send it to the server
$result = fwrite($apns, $apnsMessage, strlen($apnsMessage));

// Result returns number of bytes sent on success, false otherwise
if(intval($result) > 0){
	// Push sent
	$response = $result.' bytes sent. $payload size is: '.strlen($output).' (payload limit is 2048 bytes)';
	fclose($apns);
}else{
	// Push failed
	$error = error_get_last();
	$response = (is_array($error) ? $error['message'] : 'undefined error');
}

// Show result
die($response);

@imhsgoraya
Copy link

imhsgoraya commented Aug 16, 2018

@andreszs, i m getting "undefined error" can you help what is the issue ?

@bhavin-chauhan
Copy link

bhavin-chauhan commented Mar 12, 2019

got some errors
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /customers/d/b/3/server.com/httpd.www/app/apn-server.php on line 18 Warning: fwrite() expects parameter 1 to be resource, boolean given in /customers/d/b/3/server.com/httpd.www/app/apn-server.php on line 19 Warning: fclose() expects parameter 1 to be resource, boolean given in /customers/d/b/3/server.com/httpd.www/app/apn-server.php on line 20
Can anyone suggest what's wrong with this server.
If there is any restrictions from server then let me know what should I do.

@Harithareddykuram
Copy link

Harithareddykuram commented May 18, 2019

i am getting fwrite(): send of 92 bytes failed with errno=10053 An established connection was aborted by the software in your host machine. this error please help me.

@MOBitooT
Copy link

MOBitooT commented Jun 21, 2019

I am getting same errors as you @bhavin-chauhan. Did you manage to get this issue fixed?

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 17
Warning: pack(): Type H: illegal hex digit h in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 34

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Warning: Cannot modify header information - headers already sent by (output started at /home/mandin/public_html/vadmin2/send_broadcast_dev.php:42) in /home/mandin/public_html/vadmin2/common.php on line 977

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Can anyone help please???

@bhavin-chauhan
Copy link

bhavin-chauhan commented Jun 21, 2019

I am getting same errors as you @bhavin-chauhan. Did you manage to get this issue fixed?

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 17
Warning: pack(): Type H: illegal hex digit h in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 34

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Warning: Cannot modify header information - headers already sent by (output started at /home/mandin/public_html/vadmin2/send_broadcast_dev.php:42) in /home/mandin/public_html/vadmin2/common.php on line 977

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Can anyone help please???

My Server not allow me to open APN Port number, So Finally I've Used Firebase service for that task. It's easy to use and both android, iOS token can easily work now. Let me know if you need any help from my side.
Thanks!
Bhavin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment