Apple Push Notify

<?php function _push_to_apple($arr_device_token=array(),$data="NA",$alert="New Deal Available",$message="New Push Notification!") { foreach ($arr_device_token as $key => $deviceToken) { // My private key's passphrase here: $passphrase = '123456'; //badge $badge = 1; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_cert/ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); $body['aps'] = array( 'alert' => $alert_msg, 'badge' => $badge, 'sound' => 'default', 'data' =>$data ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); fclose($fp); } }
Above code can be used to send push notification to apple devices

Make sure you place *.pem file (obtained while registering app in developers account) at following location (at the root of project ) "apple_cert/ck.pem" and it is accessible

Example: For Codeigniter Framework in PHP you can place

--application
--system
--apple_cert


Parameters:

$arr_device_token = Array of Device Token
$data = (optional)
$alert = String ( Alert Title )
$message = String (Actual Message )

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.