Location: PHPKode > projects > Banshee PHP Framework > libraries/apns.php
<?php
	/* libraries/apns.php
	 *
	 * Copyright (C) by Hugo Leisink <hide@address.com>
	 * This file is part of the Banshee PHP framework
	 * http://www.banshee-php.org/
	 */

	class apns {
		#private $host = "gateway.push.apple.com";
		private $host = "gateway.sandbox.push.apple.com";
		private $port = 2195;
		private $certificate = null;
		private $notifications = array();

		/* Constructor
		 *
		 * INPUT:  string certificate filename
		 * OUTPUT: -
		 * ERROR:  -
		 */
		public function __construct($certificate) {
			$this->certificate = $certificate;
		}

		/* Destructor
		 *
		 * INPUT:  -
		 * OUTPUT: -
		 * ERROR:  -
		 */
		public function __destruct() {
			$this->send();
		}

		/* Add notification to queue
		 *
		 * INPUT:  mixed device token, string content
		 * OUTPUT: true
		 * ERROR:  false
		 */
		public function send_notification($tokens, $content) {
			$content = json_encode($content);
			if (($conlen = strlen($content)) > 256) {
				return false;
			}

			if (is_array($tokens) == false) {
				$tokens = array($tokens);
			}

			foreach ($tokens as $token) {
				if (($toklen = strlen($token)) != 32) {
					continue;
				}

				$notification = chr(0).
					chr(0).chr($toklen).pack("H*", $token).
					chr(0).chr($conlen).$content;
				array_push($this->notifications, $notification);
			}

			return true;
		}

		/* Send the notifications
		 *
		 * INPUT:  -
		 * OUTPUT: -
		 * ERROR:  -
		 */
		private function send() {
			if (count($this->notifications) == 0) {
				return;
			}

			$context = stream_context_create();
			stream_context_set_option($context, "ssl", "local_cert", $this->certificate);

			$uri = "ssl://".$this->host.":".$this->port;
			$socket = stream_socket_client($uri, $errno, $error, 3, STREAM_CLIENT_CONNECT, $context);

			foreach ($this->notifications as $notification) {
				fwrite($socket, $notification);
			}

			fclose($socket);
		}
	}
?>
Return current item: Banshee PHP Framework