Send IOS Push Notifications in PHP

No votes yet.
Please wait...

Hello, Mates,

Recently we got stuck in a situation where we were unable to send the push notifications on an IOS device via IOS APNS built in a PHP Backend. We invested hours and hours to figure out what we were doing wrong, unlike others. So we gave it a thought and decided to post this situation and its hack/solution over the blog which would help all our newbies to get the notifications part done easily without any problems.

 

So Mates let’s start this tutorial.

 

As we know today push notifications are serving a major role in informing, intimating and educating the users about the offers deals, personalized message and much more so it has become the need of an era. So let us quickly look around how we need to implement IOS push notification part in PHP.

Some prerequisite requirements which we need are.

  1. Certificates file with .pem extension which has been issued by your IOS developer while integrating the push in the App.
  2. Pass Phrase refers to the password used while creating the certificate. In more general it’s the certificate password which the IOS developer created it can be null also.
  3. IOS Device Key which is unique for all the IOS device and is stored at your MySql database while registering the users over the App.

Now here comes the most important requirement.

 

  1. Port 2195 & 2196 should be open on your server else you will not be able to communicate with the Apple Server. Many shared hosting providers won’t allow you to open the ports for security purpose the will ask you to have a VPS or Managed Server but Hostgator is one server which opens the port upon your request even on the shared hosting server. You can figure out the more server companies providing this facility based on your experiences.

 

Upon having these 4 requirements we move on to our coding part over which we will pass our message and IOS key over which we want to send the message and set up the environment.

 

So let’s get ready.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
$deviceToken = $deviceToken;  //put your device token / IOS Key here


$message = str_replace(chr(34), "", "Hello PHP Hurdles Push Notifications");  //Encapsulate your message here

//Upload the pem file on your server and proviode the correct path
$apnsCert = $_SERVER["DOCUMENT_ROOT"].'/admin/application/controllers/Fintel_Dist.pem';

//certificate password/pass phrase
$passphrase = '';   //Enter the password here consult your ios developer for the password it can be null if he hasn;t set it.

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

//The Server which we communicate to for sending the push make sure port 2196 and 2195 are enables else it will not work


// Create the  body for sending
$body['aps'] = array(
    'badge' => +1,          
    'alert' => $message,
    'sound' => 'default'
);

//badge is the count which gets showed on the App launcher
//alert is the message you want to sending
//Sound is the poping sound of the iphone when notification comes


$payload = json_encode($body);   //encode the data in form of json




$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;


$result = fwrite($fp, $msg, strlen($msg));  //Send notification to server

if (!$result)
    $data['msg1']='Message not delivered';    //Failure
else
    $data['msg1']='Send';                     //Success in sending

// Close the connection to the server
fclose($fp);
?>

In the above example, we saw that we need a IOS Device key, A certificate which needs to be uploaded to the server and correct path should be given else an error which is not easy to figure out will be thrown. Ports 2196 & 2195 needs to be enabled on the server else you won’t be able to pass your request to the apple server.

All the above requirements along with the code can be used to create a user-friendly push notification system for IOS Platform.

You can also Download the code here to use it as your own will.

Since helping our developers is our main motive and we believe HURDLES AREN’T REALLY HURDLES

If you have any question or any suggestion do write us back at

You can also Download the Source Code Here to use it as your own will.

 

Since helping our developers is our main motive and we believe HURDLES AREN’T REALLY HURDLES

 

If you have any question or any suggestion do write us back at phphurdle@gmail.com

Cheers mates.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
29 × 14 =