1
votes

I am trying to send TOAST (PUSH) notification to Windows Phone Application (8.1) from PHP application. Configuration for the notification is done right . Configuration is validated with (http://31daysofwindows8.com/push) and it works fine. However when I use following code, I get notification as a string "New Notification". This notification does not have header, default image as it should be .Also what we observed is when XML payload is commented and a simple string is send, the same notification is receieved. I doubt the XML payload what is send is not right. Please guide me

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

The resultant XML payload is as following

<?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
According to documentation, you need to convert your string to stream. Here is a sample in C#, but i can't do it in PHP byte[] contentInBytes = Encoding.UTF8.GetBytes(xml); using (Stream requestStream = request.GetRequestStream()) requestStream.Write(contentInBytes, 0, contentInBytes.Length);grandv22

2 Answers

0
votes

Try to change your toastMessage to :

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

You can find all the toast catalogue here https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx?f=255&MSPPError=-2147217396

0
votes

Try to use below code, This code is well tested and working in many apps.

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