Windows Phone使用PHP推送TOAST通知

时间:2014-12-17 12:41:47

标签: php windows-phone-8.1

我正在尝试从PHP应用程序向Windows Phone应用程序(8.1)发送TOAST(PUSH)通知。通知的配置是正确的。配置使用(http://31daysofwindows8.com/push)验证,并且工作正常。但是,当我使用以下代码时,我会收到通知作为字符串"新通知"。此通知没有标题,默认图像应该是。我们观察到的是当注释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);

生成的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>"

2 个答案:

答案 0 :(得分:0)

尝试将toastMessage更改为:

setImageViewBitmap

您可以在https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx?f=255&MSPPError=-2147217396

找到所有的吐司目录

答案 1 :(得分:0)

尝试使用以下代码,此代码经过了充分测试,可在许多应用中使用。

public function sendPushNotification($ notify_url,$ msg,$ type){

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