Iconic LiveTile push notification via php results in PayloadFormatError

烈酒焚心 提交于 2019-12-11 06:09:32

问题


Final EDIT working code (Huge thanks to Claus for taking time and solving it):

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;
     }
}

EDIT: The XML (tried with both local image and remote image)

<?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 Full error msg:

"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 Server response

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

I'm using the above code (made by Rudy Huyn) when trying to send a Push Notification. It works fine with sending a normal tile but I'm trying to update my app so it can receive a iconic tile instead. I've added the send_iconic_tile myself.

However when I'm sending an iconic tile notification I'm getting a PayloadFormatError on the phone. I can't see any error in my code either since the payload seems to match what Microsoft has written here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207009(v=vs.105).aspx


回答1:


Your payload is indeed invalid.

The BackgroundColor needs to be in HEX format of #FF000000 (00FFFFFF for Transparent), and the Id="/" should be Id="".

Remember there can't be any whitespaces before or after the hex value for the background color.



来源:https://stackoverflow.com/questions/13267261/iconic-livetile-push-notification-via-php-results-in-payloadformaterror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!