使用PHP的Windows Phone推送TOAST通知

使用PHP的Windows Phone推送TOAST通知,php,windows-phone-8.1,Php,Windows Phone 8.1,我正在尝试从PHP应用程序向Windows Phone应用程序(8.1)发送TOAST(推送)通知。通知的配置已正确完成。配置通过()进行验证,工作正常。然而,当我使用下面的代码时,我得到的通知是字符串“newnotification”。此通知没有标题,默认图像应该是这样的。我们还观察到,当XML负载被注释并发送一个简单字符串时,接收到相同的通知。我怀疑发送的XML负载是不对的。请引导我 $tokenRequest = curl_init(); curl_setopt($tokenReq

我正在尝试从PHP应用程序向Windows Phone应用程序(8.1)发送TOAST(推送)通知。通知的配置已正确完成。配置通过()进行验证,工作正常。然而,当我使用下面的代码时,我得到的通知是字符串“newnotification”。此通知没有标题,默认图像应该是这样的。我们还观察到,当XML负载被注释并发送一个简单字符串时,接收到相同的通知。我怀疑发送的XML负载是不对的。请引导我

$tokenRequest = curl_init();
    curl_setopt($tokenRequest, CURLOPT_URL, 'https://login.live.com/accesstoken.srf');

    curl_setopt($tokenRequest, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));

    //FIELDS
    $fields = array(
        'grant_type' => 'client_credentials',
        'client_id' => 'our client id',
        'client_secret' => 'our client secret',
        'scope' => 'notify.windows.com'
    );

    $fields_string = "";
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    curl_setopt($tokenRequest, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($tokenRequest,CURLOPT_POST, count($fields));
    curl_setopt($tokenRequest,CURLOPT_POSTFIELDS, $fields_string);

    $output = json_decode(curl_exec($tokenRequest));
    curl_close($tokenRequest);
    echo "<br>";
    echo "<br>";
    $accessToken = $output->{'access_token'};

 $sendPush = curl_init();
 curl_setopt($sendPush, CURLOPT_URL, 'our URI here');
 //TOAST MESSAGE
    $toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
    "<wp:Notification xmlns:wp=\"WPNotification\">" .
    "<wp:Toast>" .
    "<wp:Text1>Text...</wp:Text1>" .
    "<wp:Text2>text..</wp:Text2>" .
    "</wp:Toast>" .
    "</wp:Notification>";
    curl_setopt($sendPush, CURLOPT_HEADER, true);
 echo $toastMessage;
    $headers = array('Content-Type: text/xml',,"Content-Type: text/xml", "X-WNS-Type: wns/toast","Content-Length: " . strlen($toastMessage),"X-NotificationClass:2" ,"X-WindowsPhone-Target: toast","Authorization: Bearer $accessToken");
   curl_setopt($sendPush, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($sendPush, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($sendPush,CURLOPT_POST, true);        

 curl_setopt($sendPush, CURLOPT_POSTFIELDS, $toastMessage));


    $output = curl_exec($sendPush);
 $info = curl_getinfo($sendPush);

 echo($info['request_header']);
    echo "<br>";
    //var_dump(curl_getinfo($sendPush, CURLINFO_HTTP_CODE));
    echo "<br>";
    //var_dump(curl_getinfo($sendPush, CURLINFO_HEADER_OUT));
    echo "<br>";

    curl_close($sendPush);
$tokenRequest=curl_init();
curl_setopt($tokenRequest,CURLOPT_URL,'https://login.live.com/accesstoken.srf');
curl_setopt($tokenRequest,CURLOPT_HTTPHEADER,数组(
'内容类型:application/x-www-form-urlencoded'
));
//田地
$fields=数组(
“授权类型”=>“客户端凭据”,
'客户id'=>'我们的客户id',
“客户机密”=>“我们的客户机密”,
'scope'=>'notify.windows.com'
);
$fields_string=“”;
foreach($key=>$value形式的字段){$fields_string.=$key.'='.$value.&';}
rtrim($fields_string,&');
curl_setopt($tokenRequest,CURLOPT_RETURNTRANSFER,true);
curl_setopt($tokenRequest,CURLOPT_POST,count($fields));
curl_setopt($tokenRequest,CURLOPT_POSTFIELDS,$fields_string);
$output=json_解码(curl_exec($tokenRequest));
卷曲关闭($tokenRequest);
回声“
”; 回声“
”; $accessToken=$output->{'access\u token'}; $sendPush=curl_init(); curl_setopt($sendPush,CURLOPT_URL,“我们的URI在这里”); //祝酒词 $toastMessage=”“。 "" . "" . “文本…”。 “文本…”。 "" . ""; curl_setopt($sendPush,CURLOPT_头,true); echo$toastMessage; $headers=array('Content-Type:text/xml',“Content-Type:text/xml”,“X-WNS-Type:WNS/toast”,“Content-Length:.strlen($toastMessage),“X-NotificationClass:2”,“X-WindowsPhone-Target:toast”,“Authorization:Bearer$accessToken”); curl_setopt($sendPush,CURLOPT_HTTPHEADER,$headers); curl_setopt($sendPush,CURLOPT_RETURNTRANSFER,true); curl_setopt($sendPush,CURLOPT_POST,true); curl_setopt($sendPush,CURLOPT_POSTFIELDS,$toastMessage)); $output=curl\u exec($sendPush); $info=curl\u getinfo($sendPush); echo($info['request_header']); 回声“
”; //var_dump(curl_getinfo($sendPush,CURLINFO_HTTP_代码)); 回声“
”; //var_dump(curl_getinfo($sendPush,CURLINFO_HEADER_OUT)); 回声“
”; 卷曲关闭($sendPush);
生成的XML负载如下所示

<?xml version=\"1.0\" encoding=\"utf-8\"?>
                    <wp:Notification 
                        xmlns:wp=\"WPNotification\">
                        <wp:Toast>
                            <wp:Text1>Sharvin</wp:Text1>
                            <wp:Text2>Notif</wp:Text2>
                        </wp:Toast>
                    </wp:Notification>"

沙尔温
诺蒂夫
"

尝试将演讲信息更改为:

$toastMessage = "<toast>".
            "<visual>".
                "<binding template=\"ToastText01\">".
                    "<text id=\"1\">".$message."</text>".
                "</binding>".
            "</visual>".
        "</toast>";
$toastMessage=”“。
"".
"".
“.$message.”。
"".
"".
"";

您可以在这里找到所有的toast目录

尝试使用下面的代码,这些代码经过了良好的测试,在许多应用程序中都能正常工作。

公共函数sendPushNotification($notify\u url,$msg,$type){

$delay=1;
$msg=”“。
"" .
"" .
“.$type.”。
“.$msg.”。
"" .
"";
$sendedheaders=数组(
'内容类型:text/xml',
“接受:应用程序/*”,
“X-WindowsPhone-Target:toast”,
“X-NotificationClass:$delay”
);
$req=curl_init();
curl_setopt($req,CURLOPT_头,true);
curl_setopt($req,CURLOPT_HTTPHEADER,$sendeheaders);
curl_setopt($req,CURLOPT_POST,true);
curl_setopt($req,CURLOPT_POSTFIELDS,$msg);
curl_setopt($req,CURLOPT_URL,$notify_URL);
curl_setopt($req,CURLOPT_RETURNTRANSFER,1);
$response=curl\u exec($req);
//回声';
//打印($response);死亡;
卷曲关闭($req);
}

根据文档,您需要将字符串转换为流。这是C#中的一个示例,但我无法在PHP byte[]contentInBytes=Encoding.UTF8.GetBytes(xml);使用(stream requestStream=request.GetRequestStream())requestStream.Write(contentInBytes,0,contentInBytes.Length);
    $delay = 1;
    $msg =  "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
            "<wp:Notification xmlns:wp=\"WPNotification\">" .
                "<wp:Toast>" .
                    "<wp:typessss>".$type."</wp:typessss>" .
                    "<wp:Datassss>".$msg."</wp:Datassss>" .
                "</wp:Toast>" .
            "</wp:Notification>";

    $sendedheaders =  array(
        'Content-Type: text/xml',
        'Accept: application/*',
        'X-WindowsPhone-Target: toast',
        "X-NotificationClass: $delay"
    );

        $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, $notify_url);
        curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($req);
        //echo '<pre>';
        //print_r($response); die;
        curl_close($req);       
}