title: Convert a "php script to send push notification" in android http post
body: <p>I want to convert <strong>a php script</strong>(to send push notification in android devices) 
  in to <strong>a android http post</strong> code.</p>
  
  <p><strong>php script (working fine)</strong> :</p>
  
  <p><strong>printed response :</strong> </p>
  
  <p>{"multicast_id":7657471480142898452,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1362731818787207%38894628f9fd7ecd"}]}</p>
  
  <pre><code> &lt;?php
   $registrationIds = array("APA91bH1-35qLot...."); //device registration id
  
  $apiKey = "AIzaSyCrKrzFl...";   //"YOUR_BROWSER_API_KEY";
  
  $response = sendNotification($apiKey,$registrationIds,array('message' =&gt; $message, 'tickerText' =&gt; $tickerText, 'contentTitle' =&gt; $contentTitle, "contentText" =&gt; $contentText) );
  
  echo $response;
  
  function sendNotification( $apiKey, $registrationIdsArray, $messageData ) {
  $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
  $data = array(
  'data' =&gt; $messageData,
  'registration_ids' =&gt; $registrationIdsArray
  );
  
  $ch = curl_init();
  
  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
  
  $response = curl_exec($ch);
  curl_close($ch);
  
  return $response;
  
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>Android code (not working):</strong></p>
  
  <p><strong>printed response :</strong></p>
  
  <pre><code>&lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;
  &lt;/HEAD&gt;
   &lt;BODY BGCOLOR="#FFFFFF" TEXT="#000000"&gt;
   &lt;H1&gt;Unauthorized&lt;/H1&gt;
  &lt;H2&gt;Error 401&lt;/H2&gt;
   &lt;/BODY&gt;
   &lt;/HTML&gt;
  
  public boolean sendMessage() {
  
          String serviceUrl = "https://android.googleapis.com/gcm/send";
  
          ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
          nameValuePairs.add(new BasicNameValuePair("registration_ids", "APA91bH1-35qLotyb......"));
          nameValuePairs.add(new BasicNameValuePair("data", "this is a test message"));
  
          try {
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(serviceUrl);
              httpPost.setHeader("Authorization", "AIzaSyCrKrzFlPbauq......");
              httpPost.setHeader("Content-Type", "application/json");
              httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              json_str = EntityUtils.toString(httpEntity);
  
          } catch (UnsupportedEncodingException e) {
              json_str = "Can't connect to server";
          } catch (MalformedURLException e) {
              json_str = "Can't connect to server";
          } catch (IOException e) {
              json_str = "Can't connect to server";
          }
  
          Log.i("***sendMessage", json_str);
          return true;
      }
  </code></pre>