最终编辑工作代码(非常感谢克劳斯花时间解决它):
class WindowsPhonePushDelay
{
const Immediate=0;
const In450Sec=10;
const In900Sec=20;
}
class WindowsPhonePushNotification
{
private $notif_url = '';
function WindowsPhonePushNotification($notif_url)
{
$this->notif_url = $notif_url;
}
public function send_raw($msg,$message_id=NULL, $delay = WindowsPhonePushDelay::Immediate)
{
return $this->send_push(NULL,$delay+3,$message_id, $msg);
}
public function send_normal_tile($image_url, $title, $count,$message_id=NULL, $delay = WindowsPhonePushDelay::Immediate)
{
$msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
"<wp:Notification xmlns:wp=\"WPNotification\">" .
"<wp:Tile>" .
"<wp:BackgroundImage>$image_url</wp:BackgroundImage>" .
"<wp:Count>$count</wp:Count>" .
"<wp:Title>$title</wp:Title>" .
"</wp:Tile>" .
"</wp:Notification>";
return $this->send_push('token',$delay+1, $message_id,$msg);
}
public function send_iconic_tile($image_url_large, $image_url_small, $wide1, $wide2, $wide3, $title, $count,$message_id=NULL, $delay = WindowsPhonePushDelay::Immediate)
{
$msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
"<wp:Notification xmlns:wp=\"WPNotification\" Version=\"2.0\">" .
"<wp:Tile Id=\"\" Template=\"IconicTile\">" .
"<wp:SmallIconImage>$image_url_small</wp:SmallIconImage>" .
"<wp:IconImage>$image_url_large</wp:IconImage>" .
"<wp:WideContent1>$wide1</wp:WideContent1>" .
"<wp:WideContent2>$wide2</wp:WideContent2>" .
"<wp:WideContent3>$wide3</wp:WideContent3>" .
"<wp:Count>$count</wp:Count>" .
"<wp:Title>$title</wp:Title>" .
"<wp:BackgroundColor>#00FFFFFF</wp:BackgroundColor>" .
"</wp:Tile>" .
"</wp:Notification>";
echo $msg;
return $this->send_push('token',$delay+1, $message_id,$msg);
}
public function send_toast($title, $message,$message_id=NULL, $delay = WindowsPhonePushDelay::Immediate)
{
$msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
"<wp:Notification xmlns:wp=\"WPNotification\">" .
"<wp:Toast>" .
"<wp:Text1>$title</wp:Text1>" .
"<wp:Text2>$message</wp:Text2>" .
"</wp:Toast>" .
"</wp:Notification>";
return $this->send_push('toast',$delay+2,$message_id, $msg);
}
private function send_push($target,$delay,$message_id,$msg)
{
$sendedheaders= array(
'Content-Type: text/xml; charset=utf-8 ',
'Accept: application/*',
"X-NotificationClass: $delay"
);
if($message_id!=NULL)
$sendedheaders[]="X-MessageID: $message_id";
if($target!=NULL)
$sendedheaders[]="X-WindowsPhone-Target:$target";
$req = curl_init();
curl_setopt($req, CURLOPT_HEADER, true);
curl_setopt($req, CURLOPT_HTTPHEADER,$sendedheaders);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $msg);
curl_setopt($req, CURLOPT_URL, $this->notif_url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($req);
curl_close($req);
$result=array();
foreach(explode("\n",$response) as $line)
{
$tab=explode(":",$line,2);
if(count($tab)==2)
$result[$tab[0]]=trim($tab[1]);
}
return $result;
}
}
编辑:XML(尝试使用本地图像和远程图像)
<?xml version="1.0" encoding="utf-16"?><wp:Notification xmlns:wp="WPNotification" Version="2.0"><wp:Tile Id="/" Template="IconicTile"><wp:SmallIconImage Action="Clear">/Images/LiveTiles/asot_iconic_small.png</wp:SmallIconImage><wp:IconImage Action="Clear">/Images/LiveTiles/asot_iconic_large.png</wp:IconImage><wp:WideContent1 Action="Clear">Test1</wp:WideContent1><wp:WideContent2 Action="Clear">Test2</wp:WideContent2><wp:WideContent3 Action="Clear">Test3</wp:WideContent3><wp:Count Action="Clear">1</wp:Count><wp:Title Action="Clear">Title</wp:Title><wp:BackgroundColor Action="Clear">#00FFFFFF</wp:BackgroundColor></wp:Tile></wp:Notification>
EDIT2 完整的错误信息:
"The XML payload contains invalid or improperly formatted XML or the notification type specified in the header does not match the payload type used. The channel has been closed. Check your XML payload for errors and reopen the channel to obtain a new URI."
EDIT3 服务器响应
HTTP/1.1 200 OK Cache-Control: private Server: Microsoft-IIS/7.5 X-DeviceConnectionStatus: Connected X-NotificationStatus: Received X-SubscriptionStatus: Active X-MessageID: 00000000-0000-0000-0000-000000000000 ActivityId: 5bd4c4b6-1d79-4964-9a56-7d49e2aa5972 X-Server: DB3MPNSM018 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 07 Nov 2012 19:35:59 GMT Content-Length: 0
我在尝试发送推送通知时使用了上面的代码(由 Rudy Huyn 制作)。它可以正常发送普通磁贴,但我正在尝试更新我的应用程序,以便它可以接收标志性磁贴。我自己添加了 send_iconic_tile。
但是,当我发送标志性磁贴通知时,我在手机上收到了 PayloadFormatError。我的代码中也看不到任何错误,因为有效负载似乎与微软在此处编写的内容相匹配:http: //msdn.microsoft.com/en-us/library/windowsphone/develop/jj207009 (v=vs.105 ).aspx