IOS

How to send push notification to iOS using PHP

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Sending push notification to any device is a different user experience and enhance usability. It allows app owner to send useful data and notifications to it’s user. In today’s scenario it is very important for app owners to send important information using push notification.

While sending push notifications following things must be in consideration

  • Content of the notification : Notification content is very important because, It should be specific, to the point and relevant. Sending irrelevant messages may create bad impression.
  • Timely delivery : Notification should be delivered on time because, if you are sending notification about any offer or latest update, it should be on time.
  • Real time : This is most important for e-commerce or any online service providers because, you are sending them product update, order status etc. So it should be real time. User don’t want order status update after too long.

Sending push notification to different platform may have different method to send it depends on their server structure and SDK provided by them.

Here we will talk about Sending push notification to iOS. Basically, sending push notification to iOS is sending the notification with device id to iOS APNS server. And, based upon the device Id server delivers notification to that device.

APNS

# Prerequisite

  • APNS : Sending push notification may require some pre-configuration on server like APNS (Apple Push Notification Services). For that, we need to generate/download some certificates and install it on server. That certificate is helps us to connect with APNS server.
  • Device Token : Device token is identifier for apple device and assigned per app basis when app is installed.
  • .PEM file : PEM file is certificate file which we need to generate from iOS Provisioning Portal and upload it to the server, so that server can contact to iOS APNS server.

# Creating Payload

Payload is the formatted information that is send to the server as a request. Push notification for iOS has several information in its payload like Message, Badge, Sound etc.

Here is the basic payload for push notification

<?php
$body['aps'] = array(
'alert' => trim($message),
'sound' => 'default',
);
// Encode the payload as JSON
$payload = json_encode($body);
?>

# Creating Binary Notification

After creating payload you need to create binary notification which includes device token and payload. This is required to be length of 32 byte.

<?php
$msg = chr(0) . pack('n', 32) . pack('H*', trim($deviceToken)) . pack('n', strlen($payload)) . $payload;
?>

 

# Opening Socket Connection to request the notification

Once you have prepared the binary message, you will need to open the socket to connect with the server. For testing purpose you can use sandbox URL but for production it should be live URL.

<?php
// For sandbox
$url = 'ssl://gateway.sandbox.push.apple.com:2195';
 
// for production
$url = 'ssl://gateway.push.apple.com:2195'
?>

It is very important to enable one url at a time. Now you need to connect the server with the help of certificate and open the socket connection.

<?php
$passphrase = <passphrase provided by certificate owner >;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '<path of your pem file>');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
 
// Connection to APNS server
$fp = stream_socket_client($url, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
 
$result = fwrite($fp, $msg, strlen($msg));
if (!$result){
echo 'Message not delivered' . PHP_EOL;
}
else
{
//echo 'Message successfully delivered' . PHP_EOL;
return $result;
}
// Close the connection to the server
fclose($fp);
?>

 

The above code will write binary information to APNS server and APNS server will deliver it to the device.

That’s all about the iOS push notification. Please write me back in comment section about this article or any correction required.

You can also read : How to retrieve Emails from your Gmail account using PHP

 

Try this. Cheers!!!

 

  • Pingback: How to install RPM package on ubuntu | SPOKEN by YOU()

  • I have noticed you don’t monetize your page, don’t waste your traffic,
    you can earn extra cash every month because you’ve got
    hi quality content. If you want to know how to make extra money, search for: best adsense
    alternative Wrastain’s tools

  • I have been exploring for a little for any high quality articles or blog posts in this kind of area . Exploring in Yahoo I ultimately stumbled upon this web site. Reading this information So i¡¦m glad to show that I have an incredibly good uncanny feeling I came upon exactly what I needed. I such a lot indubitably will make certain to don¡¦t forget this web site and give it a look a relentless basis.

  • Great – I should certainly say I’m impressed with your site. I had no trouble navigating through all tabs as well as related info. The site ended up being truly simple to access. Great job.

  • Questo si che è un contenuto di valore! 🙂

  • Your style is so unique compared to many other people. Thank you for publishing when you have the opportunity,Guess I will just make this bookmarked.2

  • Gaa

    Please help

    [09-Nov-2017 01:48:17 America/Denver] PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Connection timed out) in /notification.php on line 28

    [09-Nov-2017 01:49:13 America/Denver] PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /notification.php on line 28

    • Chandan

      There must be a problem with your pem file or connection with your pem file.