public function SendWithSocket($deviceToken, $message, $alertSound='default', $unlockText = '', $badgeCount=0)
	{
		
		$gatewayUrl = $this->sandboxMode
			? "ssl://gateway.sandbox.push.apple.com:2195"
			: "ssl://gateway.push.apple.com:2195";
		
		$ctx = stream_context_create();
		stream_context_set_option($ctx, 'ssl', 'local_cert', $this->certFilePath);
		stream_context_set_option($ctx, 'ssl', 'passphrase', $this->certPassphrase);
		
		$output = new stdClass();
		$output->date = date('Y-m-d H:i:s');
		
		$fh = null;
		$errorMesssage = NULL;
		
		try {
			// Open a connection to the APNS server
			$fh = stream_socket_client(
				$gatewayUrl, $err,
				$errorMesssage, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
		}
		catch (Exception $ex) {
			$errorMesssage = $ex->getMessage();
		}
		
		if ($errorMesssage || !$fh)
		{
			$output->success = false;
			$output->message = "Connection Failed: $errorMesssage";
		}
		else
		{
			$appleExpiry = time() + (10 * 24 * 60 * 60); //Keep push alive (waiting for delivery) for 10 days

			// format the body based on whether an unlock message was specified
			$alert = $unlockText 
				? array('body'=>$message,'action-loc-key'=>$unlockText)
				: $message;

			// Create the payload body
			$body['aps'] = array(
					'alert' => $alert,
					'sound' => $alertSound,
					'badge' => (int)$badgeCount
			);
		
			// Encode the payload as JSON
			$payload = json_encode($body);
		
			// Build the binary notification
			ExceptionThrower::$IGNORE_DEPRECATED = true;
			ExceptionThrower::Start();

			try {
				
				// @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
				$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
				fwrite($fh, $msg, strlen($msg));

				$response = $this->getResponse($fh);

				if (!$response) {
					// everything is cool
					$output->success = true;
					$output->message = 'Message sent';
				}
				else {
					$output->success = false;
					$output->message = 'Push notification failed with response: ' . $response;
				}
			}
			catch (Exception $ex)
			{
				$output->success = false;
				$output->message = $ex->getMessage();
			}
			
			ExceptionThrower::Stop();
		}
		
		// Close the connection to the server
		if ($fh) @fclose($fh);
		
		return $output;
		
	}
public function SendWithSocket($deviceToken, $message, $alertSound='default', $unlockText = '', $badgeCount=0)
	{
		
		$gatewayUrl = $this->sandboxMode
			? "ssl://gateway.sandbox.push.apple.com:2195"
			: "ssl://gateway.push.apple.com:2195";
		
		$ctx = stream_context_create();
		stream_context_set_option($ctx, 'ssl', 'local_cert', $this->certFilePath);
		stream_context_set_option($ctx, 'ssl', 'passphrase', $this->certPassphrase);
		
		$output = new stdClass();
		$output->date = date('Y-m-d H:i:s');
		
		$fh = null;
		$errorMesssage = NULL;
		
		try {
			// Open a connection to the APNS server
			$fh = stream_socket_client(
				$gatewayUrl, $err,
				$errorMesssage, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
		}
		catch (Exception $ex) {
			$errorMesssage = $ex->getMessage();
		}
		
		if ($errorMesssage || !$fh)
		{
			$output->success = false;
			$output->message = "Connection Failed: $errorMesssage";
		}
		else
		{
			$appleExpiry = time() + (10 * 24 * 60 * 60); //Keep push alive (waiting for delivery) for 10 days

			// format the body based on whether an unlock message was specified
			$alert = $unlockText 
				? array('body'=>$message,'action-loc-key'=>$unlockText)
				: $message;

			// Create the payload body
			$body['aps'] = array(
					'alert' => $alert,
					'sound' => $alertSound,
					'badge' => (int)$badgeCount
			);
		
			// Encode the payload as JSON
			$payload = json_encode($body);
		
			// Build the binary notification
			ExceptionThrower::$IGNORE_DEPRECATED = true;
			ExceptionThrower::Start();

			try {
				
				// @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
				$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
				fwrite($fh, $msg, strlen($msg));

				$response = $this->getResponse($fh);

				if (!$response) {
					// everything is cool
					$output->success = true;
					$output->message = 'Message sent';
				}
				else {
					$output->success = false;
					$output->message = 'Push notification failed with response: ' . $response;
				}
			}
			catch (Exception $ex)
			{
				$output->success = false;
				$output->message = $ex->getMessage();
			}
			
			ExceptionThrower::Stop();
		}
		
		// Close the connection to the server
		if ($fh) @fclose($fh);
		
		return $output;
		
	}
public function SendWithSocket ( $ deviceToken , $ message , $ alertSound = 'default' , $ unlockText = '' , $ badgeCount = 0 ) { $ gatewayUrl = $ this -> sandboxMode ? "ssl://gateway.sandbox.push.apple.com:2195" : "ssl://gateway.push.apple.com:2195" ; $ ctx = stream_context_create ( ) ; stream_context_set_option ( $ ctx , 'ssl' , 'local_cert' , $ this -> certFilePath ) ; stream_context_set_option ( $ ctx , 'ssl' , 'passphrase' , $ this -> certPassphrase ) ; $ output = new stdClass ( ) ; $ output -> date = date ( 'Y-m-d H:i:s' ) ; $ fh = null ; $ errorMesssage = NULL ; try { // Open a connection to the APNS server
 $ fh = stream_socket_client ( $ gatewayUrl , $ err , $ errorMesssage , 60 , STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT , $ ctx ) ; } catch ( Exception $ ex ) { $ errorMesssage = $ ex -> getMessage ( ) ; } if ( $ errorMesssage || ! $ fh ) { $ output -> success = false ; $ output -> message = "Connection Failed: $errorMesssage" ; } else { $ appleExpiry = time ( ) + ( 10 * 24 * 60 * 60 ) ; //Keep push alive (waiting for delivery) for 10 days
 // format the body based on whether an unlock message was specified $ alert = $ unlockText ? array ( 'body' => $ message , 'action-loc-key' => $ unlockText ) : $ message ; // Create the payload body
 $ body [ 'aps' ] = array ( 'alert' => $ alert , 'sound' => $ alertSound , 'badge' => ( int ) $ badgeCount ) ; // Encode the payload as JSON
 $ payload = json_encode ( $ body ) ; // Build the binary notification ExceptionThrower :: $ IGNORE_DEPRECATED = true ; ExceptionThrower :: Start ( ) ; try { // @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
 $ msg = chr ( 0 ) . pack ( 'n' , 32 ) . pack ( 'H*' , $ deviceToken ) . pack ( 'n' , strlen ( $ payload ) ) . $ payload ; fwrite ( $ fh , $ msg , strlen ( $ msg ) ) ; $ response = $ this -> getResponse ( $ fh ) ; if ( ! $ response ) { // everything is cool $ output -> success = true ; $ output -> message = 'Message sent' ; } else { $ output -> success = false ; $ output -> message = 'Push notification failed with response: ' . $ response ; } } catch ( Exception $ ex ) { $ output -> success = false ; $ output -> message = $ ex -> getMessage ( ) ; } ExceptionThrower :: Stop ( ) ; } // Close the connection to the server if ( $ fh ) @ fclose ( $ fh ) ; return $ output ; }
Send a push notification directly using a socket
@param string $deviceToken the push token for the mobile device
@param string $message the message to display
@param string $alertSound the audio file to play, otherwise use the default sound
@param string $unlockText if the device is locked, show "Slide to XXX" where XXX is the unlockText
@param int $badgeCount the number that should be shown on the springboard badge
Send a push notification directly using a socket