title: How to send push notification using php in windows phone?
body: <p>Hi I need to send toast push notification from my windows phone app using PHP. I have used some codes but I can't send push notification.</p>
  
  <p>My code is given below:</p>
  
  <pre><code>        private void Click_Login(object sender, RoutedEventArgs e)
          {
          string url = "http://www.best.com/Best_Windows/Login.php";
  
          Uri uri = new Uri(url, UriKind.Absolute);
  
          StringBuilder postData = new StringBuilder();
          postData.AppendFormat("{0}={1}", "sUsername", HttpUtility.UrlEncode(this.txtEmailaddress.Text));
          postData.AppendFormat("&amp;{0}={1}", "sPassword", HttpUtility.UrlEncode(this.txtPassword.Password.ToString()));
  
          WebClient client = default(WebClient);
          client = new WebClient();
          client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
          client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
  
          client.UploadStringCompleted += client_UploadStringCompleted;
          client.UploadProgressChanged += client_UploadProgressChanged;
  
          client.UploadStringAsync(uri, "POST", postData.ToString());
  
          prog = new ProgressIndicator();
          prog.IsIndeterminate = true;
          prog.IsVisible = true;
          SystemTray.SetProgressIndicator(this, prog);
  
      }
  
  
      private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
      {
          if (e.Cancelled == false &amp; e.Error == null)
          {
              prog.IsVisible = false;
  
              string[] result = e.Result.ToString().Split('|');
              string strStatus = result[0].ToString();
              strcustomer_id_k = result[1].ToString();
              string strError = result[2].ToString();
  
              if (strStatus == "0")
              {
                  MessageBox.Show(strError);
                  LoginPage.strcustomer_id_k = null;
              }
              else
              {
                  save_pendingmessage();
              }     
  
  
  
  
         public void save_pendingmessage()
         { 
  
           HttpNotificationChannel pushChannel;
  
          // The name of our push channel.
          string channelName = "ToastSampleChannel";
  
          // Try to find the push channel.
          pushChannel = HttpNotificationChannel.Find(channelName);
          // If the channel was not found, then create a new connection to the push service.
          if (pushChannel == null)
          {
              pushChannel = new HttpNotificationChannel(channelName);
  
              // Register for all the events before attempting to open the channel.
              pushChannel.ChannelUriUpdated += new EventHandler&lt;NotificationChannelUriEventArgs&gt;(PushChannel_ChannelUriUpdated);
              pushChannel.ErrorOccurred += new EventHandler&lt;NotificationChannelErrorEventArgs&gt;(PushChannel_ErrorOccurred);
  
              // Register for this notification only if you need to receive the notifications while your application is running.
              pushChannel.ShellToastNotificationReceived += new EventHandler&lt;NotificationEventArgs&gt;(PushChannel_ShellToastNotificationReceived);
  
              pushChannel.Open();
  
              // Bind this new channel for toast events.
              pushChannel.BindToShellToast();
  
          }
          else
          {
              // The channel was already open, so just register for all the events.
              pushChannel.ChannelUriUpdated += new EventHandler&lt;NotificationChannelUriEventArgs&gt;(PushChannel_ChannelUriUpdated);
              pushChannel.ErrorOccurred += new EventHandler&lt;NotificationChannelErrorEventArgs&gt;(PushChannel_ErrorOccurred);
  
              // Register for this notification only if you need to receive the notifications while your application is running.
              pushChannel.ShellToastNotificationReceived += new EventHandler&lt;NotificationEventArgs&gt;(PushChannel_ShellToastNotificationReceived);
  
              // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
              System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
  
          }            
  
          string url = "http://www.best.com/Best_Windows/send_message.php";
          Uri uri = new Uri(url, UriKind.Absolute);
  
  
          StringBuilder postData = new StringBuilder();
          postData.AppendFormat("{0}={1}", "customer_id", HttpUtility.UrlEncode(strcustomer_id_k));
          postData.AppendFormat("&amp;{0}={1}", "device_uri", HttpUtility.UrlEncode(pushChannel.ChannelUri.ToString()));
          WebClient client = default(WebClient);
          client = new WebClient();
          client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
          client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
  
          client.UploadStringCompleted += client_SaveStringCompleted;
  
          client.UploadStringAsync(uri, "POST", postData.ToString());
  
          prog = new ProgressIndicator();
          prog.IsVisible = true;
         // prog.Text = "Loading....";
      }
      private void client_SaveStringCompleted(object sender, UploadStringCompletedEventArgs e)
      {
  
          if (e.Cancelled == false &amp;&amp; e.Error == null)
          {
              string[] result = e.Result.ToString().Split('|');
              //*** Check Status
              if (result[0].ToString() == "0")
              {
                  MessageBox.Show(result[0].ToString());
              }
              else
              {
                  MessageBox.Show(result[0].ToString());
                 // NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
              }
  
              prog.IsVisible = false;
          }
      }  
  </code></pre>
  
  <p>My php codes given below</p>
  
  <p><strong>send_message.php</strong></p>
  
  <pre><code>&lt;?php
   $con=mysqli_connect("localhost","user","pass","dbname");
   if (isset($_POST["customer_id"]) &amp;&amp; isset($_POST["device_uri"])) 
   {
    $notif_url = $_POST["device_uri"];
    $customer_id =  $_POST["customer_id"];
  
    $getregid = mysqli_query($con,"select * from pim_offerstatus where customer_id='6' and device_uri = 'fghfhghfghfgh' ");
    if (mysqli_num_rows($getregid) &gt; 0) 
    {
     $query = mysqli_query($con,"select count(offer) as count from pim_offerstatus where         customer_id='6' and offer!=''");
     $count = mysqli_fetch_array($query);
     $msg = $count['count'];
     $message = array("offer" =&gt; $msg." "."The offers received");
     include_once './windowspush.php';
     $windows_push = new WindowsPhonePushNotification($notif_url);
     $res = $windows_push-&gt;push($target,$message,$notif_url);
  
     $response["success"] = 1;
     $response["message"] = "gum registration id is successfully updated";
      // echoing JSON response
    echo json_encode($response);
    }
  }
    else 
   {
    required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
  
    echoing JSON response
    echo json_encode($response);
   }
   ?&gt;
  </code></pre>
  
  <p><strong>windowsphone.php</strong></p>
  
  <pre><code>&lt;?php 
   final class WindowsPhonePushDelay
   {
  
    const Immediate=0;
  
    private function __construct(){}
  }
  
  class WindowsPhonePushNotification
  {
    private $notif_url = '';
  
    function WindowsPhonePushNotification($notif_url)
    {
      $this-&gt;notif_url = $notif_url;
    }
    public function push($target,$message_id,$message1,$notif_url)
    {
  
    // Create the toast message
    $toastMessage = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;" .
              "&lt;wp:Notification xmlns:wp=\"WPNotification\"&gt;" .
                 "&lt;wp:Toast&gt;" .
                      "&lt;wp:Text1&gt;" . "SendToast" . "&lt;/wp:Text1&gt;" .
                      "&lt;wp:Text2&gt;" . $message1 . "&lt;/wp:Text2&gt;" .
                      ""&lt;wp:Param&gt;/BestDeal.xaml?=Toast Notification&lt;/wp:Param&gt;" .
                      "&lt;/wp:Toast&gt; " .
              "&lt;/wp:Notification&gt;";
  
     // Create request to send
     $r = curl_init();
     curl_setopt($r, CURLOPT_URL,$notif_url);
     curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($r, CURLOPT_POST, true);
     curl_setopt($r, CURLOPT_HEADER, true); 
  
    // add headers
     $httpHeaders=array('Content-type: text/xml; charset=utf-8', 'X-WindowsPhone-Target: toast',
                  'Accept: application/*', 'X-NotificationClass: 2','Content-Length:'.strlen($toastMessage));
  curl_setopt($r, CURLOPT_HTTPHEADER, $httpHeaders);
  
  // add message
  curl_setopt($r, CURLOPT_POSTFIELDS, $toastMessage);
  
  // execute request
  $output = curl_exec($r);
  curl_close($r);
  
   }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Hi this is my code, I need when the customer log in successfully while send receive push notification in my app. Can you tell me why I am facing this issue and how to solve it?</p>