【问题标题】:Windows Phone Push TOAST Notification using PHPWindows Phone 使用 PHP 推送 TOAST 通知
【发布时间】:2014-12-17 12:41:46
【问题描述】:

我正在尝试从 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>"

【问题讨论】:

  • 根据文档,您需要将字符串转换为流。这是 C# 中的示例,但我不能在 PHP byte[] contentInBytes = Encoding.UTF8.GetBytes(xml);使用 (Stream requestStream = request.GetRequestStream()) requestStream.Write(contentInBytes, 0, contentInBytes.Length);

标签: php windows-phone-8.1


【解决方案1】:

尝试将您的 toastMessage 更改为:

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

你可以在这里找到所有的吐司目录https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx?f=255&MSPPError=-2147217396

【讨论】:

    【解决方案2】:

    尝试使用以下代码,此代码经过良好测试,可在许多应用中运行。

    公共函数 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);       
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-13
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多