title: Android Push Notification using GCM, PHP and MySQL
body: <p>I am having issue with push notifications. I am trying Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL following instructions mentioned in AndroidHive. My device is registered with GCM and can see the record in the database however when send push notification, not able to receive on the device. I am posting the link which i have followed. Please advise if i am missing anything.  <a href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/" rel="nofollow">http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/</a></p>
  
  <p>Manifest file:</p>
  
  <pre><code>AndroidManifest.xml
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidhive.pushnotifications"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;!-- GCM requires Android SDK version 2.2 (API level 8) or above. --&gt;
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="16" /&gt;
  
      &lt;!-- GCM connects to Internet Services. --&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;!-- GCM requires a Google account. --&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  
      &lt;!-- Keeps the processor from sleeping when a message is received. --&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
      &lt;!-- Creates a custom permission so only this app can receive its messages. --&gt;
      &lt;permission
          android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
  
      &lt;uses-permission android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" /&gt;
  
      &lt;!-- This app has permission to register and receive data message. --&gt;
      &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  
      &lt;!-- Network State Permissions to detect Internet status --&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;!-- Permission to vibrate --&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  
      &lt;!-- Main activity. --&gt;
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;!-- Register Activity --&gt;
          &lt;activity
              android:name=".RegisterActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;!-- Main Activity --&gt;
          &lt;activity
              android:name=".MainActivity"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name="com.google.android.gcm.GCMBroadcastReceiver"
              android:permission="com.google.android.c2dm.permission.SEND" &gt;
              &lt;intent-filter&gt;
  
                  &lt;!-- Receives the actual messages. --&gt;
                  &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
                  &lt;!-- Receives the registration id. --&gt;
                  &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
  
                  &lt;category android:name="com.androidhive.pushnotifications" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;service android:name=".GCMIntentService" /&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  
  
  index.php
  
  index.php
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              $(document).ready(function(){
  
              });
              function sendPushNotification(id){
                  var data = $('form#'+id).serialize();
                  $('form#'+id).unbind('submit');               
                  $.ajax({
                      url: "send_message.php",
                      type: 'GET',
                      data: data,
                      beforeSend: function() {
  
                      },
                      success: function(data, textStatus, xhr) {
                            $('.txt_message').val("");
                      },
                      error: function(xhr, textStatus, errorThrown) {
  
                      }
                  });
                  return false;
              }
          &lt;/script&gt;
          &lt;style type="text/css"&gt;
              .container{
                  width: 950px;
                  margin: 0 auto;
                  padding: 0;
              }
              h1{
                  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                  font-size: 24px;
                  color: #777;
              }
              div.clear{
                  clear: both;
              }
              ul.devices{
                  margin: 0;
                  padding: 0;
              }
              ul.devices li{
                  float: left;
                  list-style: none;
                  border: 1px solid #dedede;
                  padding: 10px;
                  margin: 0 15px 25px 0;
                  border-radius: 3px;
                  -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                  -moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                  color: #555;
              }
              ul.devices li label, ul.devices li span{
                  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                  font-size: 12px;
                  font-style: normal;
                  font-variant: normal;
                  font-weight: bold;
                  color: #393939;
                  display: block;
                  float: left;
              }
              ul.devices li label{
                  height: 25px;
                  width: 50px;               
              }
              ul.devices li textarea{
                  float: left;
                  resize: none;
              }
              ul.devices li .send_btn{
                  background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
                  background: -webkit-linear-gradient(0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
                  background: -moz-linear-gradient(center top, #0096FF, #005DFF);
                  background: linear-gradient(#0096FF, #005DFF);
                  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
                  border-radius: 3px;
                  color: #fff;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;?php
          include_once 'db_functions.php';
          $db = new DB_Functions();
          $users = $db-&gt;getAllUsers();
          if ($users != false)
              $no_of_users = mysql_num_rows($users);
          else
              $no_of_users = 0;
          ?&gt;
          &lt;div class="container"&gt;
              &lt;h1&gt;No of Devices Registered: &lt;?php echo $no_of_users; ?&gt;&lt;/h1&gt;
              &lt;hr/&gt;
              &lt;ul class="devices"&gt;
                  &lt;?php
                  if ($no_of_users &gt; 0) {
                      ?&gt;
                      &lt;?php
                      while ($row = mysql_fetch_array($users)) {
                          ?&gt;
                          &lt;li&gt;
                              &lt;form id="&lt;?php echo $row["id"] ?&gt;" name="" method="post" onsubmit="return sendPushNotification('&lt;?php echo $row["id"] ?&gt;')"&gt;
                                  &lt;label&gt;Name: &lt;/label&gt; &lt;span&gt;&lt;?php echo $row["name"] ?&gt;&lt;/span&gt;
                                  &lt;div class="clear"&gt;&lt;/div&gt;
                                  &lt;label&gt;Email:&lt;/label&gt; &lt;span&gt;&lt;?php echo $row["email"] ?&gt;&lt;/span&gt;
                                  &lt;div class="clear"&gt;&lt;/div&gt;
                                  &lt;div class="send_container"&gt;                               
                                      &lt;textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"&gt;&lt;/textarea&gt;
                                      &lt;input type="hidden" name="regId" value="&lt;?php echo $row["gcm_regid"] ?&gt;"/&gt;
                                      &lt;input type="submit" class="send_btn" value="Send" onclick=""/&gt;
                                  &lt;/div&gt;
                              &lt;/form&gt;
                          &lt;/li&gt;
                      &lt;?php }
                  } else { ?&gt;
                      &lt;li&gt;
                          No Users Registered Yet!
                      &lt;/li&gt;
                  &lt;?php } ?&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Register.php</p>
  
  <pre><code>&lt;?php
  
  // response json
  $json = array();
  
  /**
   * Registering a user device
   * Store reg id in users table
   */
  if (isset($_POST["name"]) &amp;&amp; isset($_POST["email"]) &amp;&amp; isset($_POST["regId"])) {
      $name = $_POST["name"];
      $email = $_POST["email"];
      $gcm_regid = $_POST["regId"]; // GCM Registration ID
      // Store user details in db
      include_once './db_functions.php';
      include_once './GCM.php';
  
      $db = new DB_Functions();
      $gcm = new GCM();
  
      $res = $db-&gt;storeUser($name, $email, $gcm_regid);
  
      $registatoin_ids = array($gcm_regid);
      $message = array("product" =&gt; "shirt");
  
      $result = $gcm-&gt;send_notification($registatoin_ids, $message);
      $headers = array(
          'Authorization: key=' . GOOGLE_API_KEY,
          'Content-Type: application/json'
      );
  
      echo $result;
  } else {
      // user details missing
  }
  ?&gt;
  </code></pre>