【发布时间】: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