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!!!