Skip to content

Instantly share code, notes, and snippets.

@cybrox
Last active August 29, 2015 13:59
Show Gist options
  • Save cybrox/10922566 to your computer and use it in GitHub Desktop.
Save cybrox/10922566 to your computer and use it in GitHub Desktop.
PHP Push notifications handler for mobile devices
<?php
/**
* PHP mobile push notification handler
* Supports: Windows Phone
*
* Written and copyrighted 2014+
* by Sven Marc 'cybrox' Gehring
* Licensed under CC-BY
*
*/
class PushHandler {
const wphoneImmediate = 0;
const wphoneIn450Sec = 10;
const wphoneIn900Sec = 20;
private $wphonePushUrl = '';
private $wphonePushIst = '';
private $wphoneParamls = array();
/**
* @method windowsPhonePush
* @name Windows Phone Push
* @desc Send a war, tile or toast push notification to a Windows Phone device
* @note This is based on an example written by Rudy HUYN
* @param {string} pushUrl - The Microsoft push URL that will handle the actual request
* @param {string} pushType - The type of the push notification, can be 'raw', 'tile' or 'toast'
* @param {array} parameters - An array with all additional parameters
* @subparam {string} data - Raw data needed for 'raw' push
* @subparam {string} title - The title of the push request
* @subparam {string} param - The param that will be given back in a 'toast' push
* @subparam {int} delay - The delay until the push is sent
* @subparam {string} subtitle - The subtitle for a 'toast' push
* @subparam {string} message_id - An unique message ID, default value is NULL
* @subparam {string} background_url - Background Url needed for 'tile' psuh
* @return {array} response - The response Array from the Microsoft server
*/
public function windowsPhonePush($pushUrl, $pushType, $parameters){
$this->wphonePushUrl = $pushUrl;
$this->wphonePushIst = $pushType;
$this->wphoneParamls = $parameters;
$paramDatas = (!empty($parameters['data'])) ? $parameters['data'] : NULL;
$paramTitle = (!empty($parameters['title'])) ? $parameters['title'] : 'no-title';
$paramParam = (!empty($parameters['param'])) ? $parameters['param'] : 'no-parameter';
$paramDelay = (!empty($parameters['delay'])) ? $parameters['delay'] : PushHandler::wphoneImmediate;
$paramStitl = (!empty($parameters['subtitle'])) ? $parameters['subtitle'] : 'no-subtitle';
$paramMsgId = (!empty($parameters['message_id'])) ? $parameters['message_id'] : NULL;
$paramBgUrl = (!empty($parameters['background_url'])) ? $parameters['background_url'] : NULL;
$paramRtype = $pushType;
switch($pushType){
case 'toast':
$requestDelay = $paramDelay + 2;
$requestMessage = "".
"<?xml version=\"1.0\" encoding=\"utf-8\"?>".
"<wp:Notification xmlns:wp=\"WPNotification\">".
"<wp:Toast>".
"<wp:Text1>".htmlspecialchars($paramTitle)."</wp:Text1>".
"<wp:Text2>".htmlspecialchars($paramStitl)."</wp:Text2>".
# "<wp:Param>".htmlspecialchars($paramParam)."</wp:Param>".
"</wp:Toast>".
"</wp:Notification>";
break;
case 'tile':
$paramRtype = 'token';
$requestDelay = $paramDelay + 1;
$requestMessage = "".
"<?xml version=\"1.0\" encoding=\"utf-8\"?>".
"<wp:Notification xmlns:wp=\"WPNotification\">".
"<wp:Tile>".
"<wp:BackgroundImage>".htmlspecialchars($paramBgUrl)."</wp:BackgroundImage>".
"<wp:Count>$count</wp:Count>".
"<wp:Title>".htmlspecialchars($paramTitle)."</wp:Title>".
"</wp:Tile>".
"</wp:Notification>";
break;
case 'raw':
$paramRtype = NULL;
$requestDelay = $paramDelay + 3;
$requestMessage = $paramDatas;
break;
default:
return array('success' => false, 'errror' => 'invalid action');
break;
}
$requestHandler = curl_init();
$requestHeaders = array(
'Content-Type: text/xml',
'Accept: application/*',
"X-NotificationClass: $requestDelay"
);
if($paramMsgId != NULL) $requestHeaders[] = "X-MessageID: $paramMsgId";
if($paramRtype != NULL) $requestHeaders[] = "X-WindowsPhone-Target: $paramRtype";
curl_setopt($requestHandler, CURLOPT_HEADER, true);
curl_setopt($requestHandler, CURLOPT_HTTPHEADER, $requestHeaders);
curl_setopt($requestHandler, CURLOPT_POST, true);
curl_setopt($requestHandler, CURLOPT_POSTFIELDS, $requestMessage);
curl_setopt($requestHandler, CURLOPT_URL, $this->wphonePushUrl);
curl_setopt($requestHandler, CURLOPT_RETURNTRANSFER, 1);
$requestDataList = array();
$requestResponse = curl_exec($requestHandler);
curl_close($requestHandler);
foreach(explode("\n", $requestResponse) as $line){
$lineParts = explode(":", $line, 2);
if(count($lineParts) == 2) $requestDataList[$lineParts[0]]=trim($lineParts[1]);
}
return $requestDataList;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment