Um die PushNotifications zu nutzen muss das folgende getan werden:
- Anlegen einer String Variable (Beschreibung ist egal) pro WindowsPhone welches man benachrichtigen möchte
- Variable ID in den Einstellungen der App unter „Interaktionen“ - „IPS VariableID für den Remotekanal“ eintippen und oben die Pushbenachrichtigungen aktivieren
- Sound kann im Benachrichtigungszenter von Windows Phone beliebig verändert werden
Welche Benachrichtigungen gibt es?
Toast - Sichtbar im Benachrichtigungscenter, so wie all die anderen Apps auch
Tile - Text auf dem LiveTile (wenn man die App auf den Startscreen gepinnt hat und die Größe der Kachel ausreicht)
Badge - Nummer auf dem LiveTile (so wie Anzahl ungelesener Mails usw., wenn man die App auf dem Startscreen gepinnt hat)
Erstellen eines Script „WindowsNotificationClass“ mit folgendem Code
<?
class WindowsNotification
{
private $header1 = 'Content-Type: text/xml';
private $header2 = 'X-WNS-Type: ';
private $header3 = 'Authorization: Bearer ';
private $header4 = 'X-WNS-Tag: ';
private $deviceURI;
private $authToken;
private $Debug = false;
private $WNSMsgToken = "abc";
function __construct($ObjID)
{
$deviceToken = GetValueString($ObjID);
$data = explode("###",$deviceToken);
if (count($data) > 1)
{
$this->deviceURI = $data[0];
$this->authToken = $data[1];
}
}
public function EnableDebug($Debug)
{
$this->Debug = $Debug;
}
public function SetWNSMsgToken($Token)
{
$this->WNSMsgToken = "abc".$Token;
}
public function sendBagdeNotification($Value)
{
$headers = array($this->header1,$this->header2.'wns/badge',$this->header3.$this->authToken);
$body = '<?xml version="1.0" encoding="utf-8"?><badge version="1" value="'.$Value.'"/>';
if ($this->Debug)
{
echo "headers
";
print_r($headers);
echo "body
".$body."
";
}
$response = $this->sendNotification($headers,$body);
return $response;
}
public function sendToastNotification($text1,$text2)
{
$headers = array($this->header1,$this->header2.'wns/toast',$this->header3.$this->authToken);
$body = '<?xml version="1.0" encoding="utf-8"?><toast><visual><binding template="ToastText02">'.
'<text id="1">'.$text1.'</text><text id="2">'.$text2.'</text>'.
'</binding></visual></toast>';
if ($this->Debug)
{
echo "headers
";
print_r($headers);
echo "body
".$body."
";
}
$response = $this->sendNotification($headers,$body);
return $response;
}
public function sendTileNotification($text1,$text2,$text3, $text4)
{
$text1 = utf8_encode($text1);
$text2 = utf8_encode($text2);
$text3 = utf8_encode($text3);
$text4 = utf8_encode($text4);
$body_medium = '<binding template="TileSquare150x150PeekImageAndText03" fallback="TileSquarePeekImageAndText01">'.
'<image id="1" src="ms-appx:///assets/House-white.100.png" alt="ipsControlImage"/>'.
'<text id="1">'.$text1.'</text><text id="2">'.$text2.'</text><text id="3">'.$text3.'</text><text id="4">'.$text4.'</text>'.
'</binding>';
$body_large = '<binding template="TileWide310x150PeekImageAndText02" fallback="TileWidePeekImage02">'.
'<image id="1" src="ms-appx:///assets/House-white.100.png" alt="ipsControlImage"/>'.
'<text id="1">'.$text1.'</text><text id="2">'.$text2.'</text><text id="3">'.$text3.'</text><text id="4">'.$text4.'</text>'.
'</binding>';
$headers = array($this->header1,$this->header2.'wns/tile',$this->header4.$this->WNSMsgToken,$this->header3.$this->authToken );
$body = '<tile><visual version="2">'.$body_medium.$body_large.'</visual></tile>';
if ($this->Debug)
{
echo "headers
";
print_r($headers);
echo "body
".$body."
";
}
$response = $this->sendNotification($headers,$body);
return $response;
}
public function sendTileNotification_gross($text1,$text2,$text3)
{
$headers = array($this->header1,$this->header2.'wns/tile',$this->header4.$this->WNSMsgToken,$this->header3.$this->authToken );
$body = '<tile><visual version="3"><binding template="TileWide310x150IconWithBadgeAndText">'.
'<image id="1" src="ms-appx:///assets/House-white.100.png" alt="ipsControlImage"/>'.
'<text id="1">'.$text1.'</text><text id="2">'.$text2.'</text><text id="3">'.$text3.'</text>'.
'</binding></visual></tile>';
if ($this->Debug)
{
echo "headers
";
print_r($headers);
echo "body
".$body."
";
}
$response = $this->sendNotification($headers,$body);
return $response;
}
public function sendTileNotification_mittel($text1,$text2,$text3)
{
$headers = array($this->header1,$this->header2.'wns/tile',$this->header4.$this->WNSMsgToken,$this->header3.$this->authToken );
$body = '<tile><visual version="3"><binding template="TileSquare150x150IconWithBadge">'.
'<image id="1" src="ms-appx:///assets/House-white.100.png" alt="ipsControlImage"/>'.
'</binding></visual></tile>';
if ($this->Debug)
{
echo "headers
";
print_r($headers);
echo "body
".$body."
";
}
$response = $this->sendNotification($headers,$body);
return $response;
}
public function sendTileNotification_klein($text1,$text2,$text3)
{
$headers = array($this->header1,$this->header2.'wns/tile',$this->header4.$this->WNSMsgToken,$this->header3.$this->authToken );
$body = '<tile><visual version="3"><binding template="TileSquare71x71IconWithBadge"><image id="1" src="ms-appx:///assets/House-white.100.png" alt="ipsControlImage"/></binding></visual></tile>';
if ($this->Debug)
{
echo "headers
";
print_r($headers);
echo "body
".$body."
";
}
$response = $this->sendNotification($headers,$body);
return $response;
}
private function sendNotification($headers,$body)
{
if ($this->Debug)
{
echo "URL:
".$this->deviceURI."
";
}
if ($this->deviceURI != "")
{
// use Client URL Library
$ch = curl_init();
// set an options for a cURL transfer
// look options here: http://www.php.net/manual/en/function.curl-setopt.php
// and what are needed here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202970(v=vs.92).aspx
curl_setopt($ch, CURLOPT_URL, $this->deviceURI);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// perform a cURL session
$response = curl_exec($ch);
// Check for errors
if($response === FALSE)
{
echo "Error in WindowsNotifications->sendNotifications: ".curl_error($ch);
}
// close a cURL session
curl_close($ch);
if($response === FALSE)
return "Error in WindowsNotifications->sendNotifications: ".curl_error($ch);
else
return true;
}
else
return "Error in WindowsNotifications->sendNotifications: DeviceURI is null! Will skip message";
}
}
?>
Ich habe mir dann jeweils ein Script für Toast & Tile Benachrichtigungen gebaut. Beispiele folgen hier…
Beispielscript für ToastBenachrichtigungen (getriggert über eine Variableveränderung):
<?
includescript(37603 /*[Test\ipsControl\PushNotification\WindowsNotificationClass]*/);
function includeScript($scriptID)
{
$s = IPS_GetScript($scriptID);
include($s['ScriptFile']);
}
if ($_IPS['SENDER'] == "Variable") //Trigger kam von Variable
{
if ($_IPS['VARIABLE'] == 31609 /*[System\HM Statusmeldungen\Messages]*/)
{
$Devices = IPS_GetChildrenIDs ( 38012 /*[Test\ipsControl\PushNotification\Devices]*/ );
foreach($Devices as $Device)
{
$WNShub = new WindowsNotification($Device);
$WNShub->EnableDebug(false);
$WNShub->sendToastNotification("ipsControl,$_IPS['VALUE']);
}
}
}
elseif ($_IPS['SENDER'] == "Execute") //Trigger kam von Variable
{
$Devices = IPS_GetChildrenIDs ( 38012 /*[Test\ipsControl\PushNotification\Devices]*/ );
foreach($Devices as $Device)
{
$WNShub = new WindowsNotification($Device);
$WNShub->EnableDebug(true);
$WNShub->sendToastNotification("ipsControl",GetValueInteger(31609 /*[System\HM Statusmeldungen\Messages]*/));
}
}
?>
Beispielscript für LiveTile Benachrichtigung:
<?
include(IPS_GetScriptFile(37603 /*[Test\ipsControl\PushNotification\WindowsNotificationClass]*/));
if ($_IPS['SENDER'] == "Variable") //Trigger kam von Variable
{
if ($_IPS['VARIABLE'] == 31609 /*[System\HM Statusmeldungen\Messages]*/)
{
$Devices = IPS_GetChildrenIDs ( 38012 /*[Test\ipsControl\PushNotification\Devices]*/ );
foreach($Devices as $Device)
{
$WNShub = new WindowsNotification($Device);
$WNShub->EnableDebug(false);
$WNShub->sendTileNotification("blub","blub1","blub2");
}
}
}
elseif($_IPS['SENDER'] == "TimerEvent") //Trigger war ein TimerEvent
{
$light=GetValue(17849 /*[System\Wetterberechnung\Lichtmenge]*/);
$light=(int)($light*100);
$light="Licht: ".$light."%";
$Temp_outside="Temperatur: ".GetValue(58965 /*[Räume\Heizungskeller\Wetterstation\WMRS200 Temperatur- und Feuchtigkeitssensor (Außen)\Temperatur]*/) ."°C";
$rain = GetValueInteger(47494 /*[Räume\Garten\Regensensor\Kanal 1\STATE]*/);
if ($rain)
$rainTXT = "es regnet aktuell!";
else
$rainTXT = "";
$text2="Wohnzimmer: ".GetValue(38295 /*[Räume\Wohnzimmer\Heizung\Wandthermostat\Werte\TEMPERATURE]*/) ."°C";
$text3="Büro: ".GetValue(45595 /*[Räume\Büro\Heizung\Regelung\Werte\ACTUAL_TEMPERATURE]*/) ."°C";
$WindowOpen = GetValueInteger(36341 /*[System\Fenster Offen Anzeige\iWindowOpen]*/);
if ($WindowOpen > 0)
$WindowOpenTXT = $WindowOpen." Fenster geöffnet";
else
$WindowOpenTXT = "";
$Devices = IPS_GetChildrenIDs ( 38012 /*[Test\ipsControl\PushNotification\Devices]*/ );
foreach($Devices as $Device)
{
$WNShub = new WindowsNotification($Device);
$WNShub->EnableDebug(false);
$WNShub->SetWNSMsgToken("1");
$WNShub->sendTileNotification("Wetter",$Temp_outside,$light,$rainTXT);
$WNShub->SetWNSMsgToken("2");
$WNShub->sendTileNotification("Räume",$text2,$text3,$WindowOpenTXT);
}
}
elseif($_IPS['SENDER'] == "Execute")
{
$light=GetValue(17849 /*[System\Wetterberechnung\Lichtmenge]*/);
$light=(int)($light*100);
$light="Licht: ".$light."%";
$Temp_outside="Temperatur: ".GetValue(58965 /*[Räume\Heizungskeller\Wetterstation\WMRS200 Temperatur- und Feuchtigkeitssensor (Außen)\Temperatur]*/) ."°C";
$rain = GetValueInteger(47494 /*[Räume\Garten\Regensensor\Kanal 1\STATE]*/);
if ($rain)
$rainTXT = "es regnet aktuell!";
else
$rainTXT = "";
$text2="Wohnzimmer: ".GetValue(38295 /*[Räume\Wohnzimmer\Heizung\Wandthermostat\Werte\TEMPERATURE]*/) ."°C";
$text3="Büro: ".GetValue(45595 /*[Räume\Büro\Heizung\Regelung\Werte\ACTUAL_TEMPERATURE]*/) ."°C";
$WindowOpen = GetValueInteger(36341 /*[System\Fenster Offen Anzeige\iWindowOpen]*/);
if ($WindowOpen > 0)
$WindowOpenTXT = $WindowOpen." Fenster geöffnet";
else
$WindowOpenTXT = "";
$Devices = IPS_GetChildrenIDs ( 38012 /*[Test\ipsControl\PushNotification\Devices]*/ );
foreach($Devices as $Device)
{
$WNShub = new WindowsNotification($Device);
$WNShub->EnableDebug(false);
$WNShub->SetWNSMsgToken("1");
$WNShub->sendTileNotification("Wetter",$Temp_outside,$light,$rainTXT);
$WNShub->SetWNSMsgToken("2");
$WNShub->sendTileNotification("Räume",$text2,$text3,$WindowOpenTXT);
}
}
?>
Einen LiveTile durch einen langen Druck auf die App in der Appliste „Aus Startseite“ erstellen