title: How to send push notifications to multiple devices using php script using FCM?
body: <p>I'm new to push notifications using FCM from php to Android devices. From android side I have generated FCM reg_id &amp; send it over php script &amp; store into mysql database. Now, I would like to send notifications from php script to multiple android devices simultaneously.</p>
  <p>Here is the php scripts that are used during sending push notifications :</p>
  <p>1.firebase.php (<a href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/" rel="noreferrer">reference link</a>)</p>
  <pre><code> &lt;?php
  
  class Firebase {
  
  // sending push message to single user by firebase reg id
  public function send($to, $message) {
      $fields = array(
          'to' =&gt; $to,
          'data' =&gt; $message,
      );
      return $this-&gt;sendPushNotification($fields);
  }
  
  // sending push message to multiple users by firebase registration ids
  public function sendMultiple($registration_ids, $message) {
      $fields = array(
          'registration_ids' =&gt; $registration_ids,
          'data' =&gt; $message,
      );
  
      return $this-&gt;sendPushNotification($fields);
  }
  
  // function makes curl request to firebase servers
  private function sendPushNotification($fields) {
      
      require_once('config.php');
  
      // Set POST variables
      $url = 'https://fcm.googleapis.com/fcm/send';
  
      $headers = array(
          'Authorization: key=' . FIREBASE_API_KEY,
          'Content-Type: application/json'
      );
      // Open connection
      $ch = curl_init();
  
      // Set the url, number of POST vars, POST data
      curl_setopt($ch, CURLOPT_URL, $url);
  
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
      // Disabling SSL Certificate support temporarly
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  
      // Execute post
      $result = curl_exec($ch);
      // echo &quot;Result&quot;.$result;
      if ($result === FALSE) {
          die('Curl failed: ' . curl_error($ch));
      }
  
      // Close connection
      curl_close($ch);
  
      return $result;
     }
   }
  
  ?&gt;
  </code></pre>
  <p>2.push.php : (<a href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/" rel="noreferrer">reference link</a>)</p>
  <pre><code>&lt;?php
  
  class Push {
  
  // push message title
  private $title;
  private $message;
  private $image;
  // push message payload
  private $data;
  // flag indicating whether to show the push
  // notification or not
  // this flag will be useful when perform some opertation
  // in background when push is recevied
  private $is_background;
  
  function __construct() {
      
  }
  
  public function setTitle($title) {
      $this-&gt;title = $title;
  }
  
  public function setMessage($message) {
      $this-&gt;message = $message;
  }
  
  public function setImage($imageUrl) {
      $this-&gt;image = $imageUrl;
  }
  
  public function setPayload($data) {
      $this-&gt;data = $data;
  }
  
  public function setIsBackground($is_background) {
      $this-&gt;is_background = $is_background;
  }
  
  public function getPush() {
      $res = array();
      $res['data']['title'] = $this-&gt;title;
      $res['data']['is_background'] = $this-&gt;is_background;
      $res['data']['message'] = $this-&gt;message;
      $res['data']['timestamp'] = date('Y-m-d G:i:s');
      return $res;
  }
  
  }
  </code></pre>
  <p>3.test.php</p>
  <pre><code> &lt;?php
  include_once('config.php');
  require_once('DB_Functions.php');
  require_once('firebase.php');
  require_once('push.php');
  
  
  
  $db = new DB_Functions();
  $firebase = new Firebase();
  $push = new Push();
  
  if(isset($_POST['send']))
  {
      // $sendvalue = $_POST['send'];
          ChromePhp::log('send it '.$_POST['send'].&quot; user 
  category:&quot;.$_POST['user_category'].&quot; Title : &quot;.$_POST['message_title'].&quot; Message : &quot;.$_POST['message_to_send']);
  
          $ucategory = $_POST['user_category'];
          ChromePhp::log('U category '.$ucategory);
  
          // notification title
          $messageTitle = isset($_POST['message_title']) ? $_POST['message_title'] : '';
          
          // notification message
          $messageToSend = isset($_POST['message_to_send']) ? $_POST['message_to_send'] : '';
          ChromePhp::log('Message Title '.$messageTitle.&quot; Message:&quot;.$messageToSend);
  
          $userslist_with_fcm_id = $db-&gt;getUsersFCMId($ucategory);
          ChromePhp::log('FCM LIST  '.$userslist_with_fcm_id-&gt;num_rows);
          // var_dump($userslist_with_fcm_id);
  
  
          $push-&gt;setTitle($messageTitle);
          $push-&gt;setMessage($messageToSend);
  
          $push-&gt;setIsBackground(FALSE);
  
          $json = '';
          $response = '';
  
          if ($userslist_with_fcm_id-&gt;num_rows &gt; 0) {
               while ($row = mysqli_fetch_array($userslist_with_fcm_id)){
               
               ChromePhp::log('FCM ID  '.$row['fcm_id']);
               
               $json = $push-&gt;getPush();
               
               $regId = $row['fcm_id'];
               $response = $firebase-&gt;send($regId, $json);
                                              }
                                          }
                                    
         else{
                echo '&lt;h3&gt;Oops ! You got empty data&lt;/h3&gt;';
             }
          var_dump($response);       
  }
      
  ?&gt;
  
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
   &lt;head&gt;
    &lt;title&gt;Admin Panel&lt;/title&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;!-- jQuery UI --&gt;
  &lt;link href=&quot;https://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css&quot; rel=&quot;stylesheet&quot; media=&quot;screen&quot;&gt;
  
  &lt;!-- Bootstrap --&gt;
  &lt;link href=&quot;bootstrap/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;!-- styles --&gt;
  &lt;link href=&quot;css/styles.css&quot; rel=&quot;stylesheet&quot;&gt;
  
  &lt;link href=&quot;//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;link href=&quot;vendors/form-helpers/css/bootstrap-formhelpers.min.css&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;css/pure-min.css&quot;&gt;
  
  &lt;link href=&quot;css/forms.css&quot; rel=&quot;stylesheet&quot;&gt;
  
  
  &lt;!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
  &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
  &lt;!--[if lt IE 9]&gt;
    &lt;script src=&quot;https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js&quot;&gt;&lt;/script&gt;
  &lt;![endif]--&gt;
      &lt;style type=&quot;text/css&quot;&gt;
          body{
          }
          div.container{
              width: 1000px;
              margin: 0 auto;
              position: relative;
          }
          legend{
              font-size: 30px;
              color: #555;
          }
          .btn_send{
              background: #00bcd4;
          }
          label{
              margin:10px 0px !important;
          }
          textarea{
              resize: none !important;
          }
          .fl_window{
              width: 400px;
              position: absolute;
              right: 0;
              top:100px;
          }
          pre, code {
              padding:10px 0px;
              box-sizing:border-box;
              -moz-box-sizing:border-box;
              webkit-box-sizing:border-box;
              display:block; 
              white-space: pre-wrap;  
              white-space: -moz-pre-wrap; 
              white-space: -pre-wrap; 
              white-space: -o-pre-wrap; 
              word-wrap: break-word; 
              width:100%; overflow-x:auto;
          }
  
      &lt;/style&gt;
  
      &lt;script type=&quot;text/javascript&quot;&gt;
  
  
  
      function sendMessage()
      {
          // alert(&quot;method called&quot;);
          var chx = document.getElementsByTagName('input');
  
          for (var i=0; i&lt;chx.length; i++) 
          {
                  // If you have more than one radio group, also check the name attribute
                  // for the one you want as in &amp;&amp; chx[i].name == 'choose'
                  // Return true from the function on first match of a checked item
                  if (chx[i].type == 'radio' &amp;&amp; chx[i].checked) 
                  {
                      // alert(&quot;checked: &quot;+chx[i].value);
                      $.ajax({
                              url: 'test.php',
                              type: 'post',
                              data: {
                                  send:&quot;true&quot;,
                                  user_category :chx[i].value,
                                  message_title : document.getElementById('title').value,
                                  message_to_send : document.getElementById('message').value
                                    },
                              success: function(data, textStatus, jqXHR)
                                  {
                                      //data - response from server
                                      // alert(&quot;success  :&quot;.textStatus);
                                  },
                                  error: function (jqXHR, textStatus, errorThrown)
                                  {
                                      // alert(&quot;failed :&quot;.textStatus);
                                  }   
                          });
                          
                  }
      
          
      }
  }
  &lt;/script&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div class=&quot;header&quot;&gt;
       &lt;div class=&quot;container&quot;&gt;
          &lt;div class=&quot;row&quot;&gt;
             &lt;div class=&quot;col-md-5&quot;&gt;
                &lt;!-- Logo --&gt;
                &lt;div class=&quot;logo&quot;&gt;
                   &lt;h1&gt;&lt;a href=&quot;index.php&quot;&gt;Admin Panel&lt;/a&gt;&lt;/h1&gt;
                &lt;/div&gt;
             &lt;/div&gt;
             &lt;div class=&quot;col-md-5&quot;&gt;
               
             &lt;/div&gt;
             &lt;div class=&quot;col-md-6&quot;&gt;
                &lt;div class=&quot;navbar navbar-inverse&quot; role=&quot;banner&quot;&gt;
                    &lt;nav class=&quot;collapse navbar-collapse bs-navbar-collapse navbar-right&quot; role=&quot;navigation&quot;&gt;
                      &lt;ul class=&quot;nav navbar-nav&quot;&gt;
                        &lt;li class=&quot;dropdown&quot;&gt;
                          &lt;a href=&quot;#&quot; class=&quot;dropdown-toggle&quot; data-toggle=&quot;dropdown&quot;&gt;My Account &lt;b class=&quot;caret&quot;&gt;&lt;/b&gt;&lt;/a&gt;
                          &lt;ul class=&quot;dropdown-menu animated fadeInUp&quot;&gt;
                              &lt;li&gt;&lt;a href=&quot;updateProfile.php&quot;&gt;Update Profile&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;a href=&quot;logout.php&quot;&gt;Logout&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                        &lt;/li&gt;
                      &lt;/ul&gt;
                    &lt;/nav&gt;
                &lt;/div&gt;
             &lt;/div&gt;
          &lt;/div&gt;
       &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class=&quot;page-content&quot;&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col-md-2&quot;&gt;
          &lt;div class=&quot;sidebar content-box&quot; style=&quot;display: block;&quot;&gt;
              &lt;ul class=&quot;nav&quot;&gt;
                  &lt;!-- Main menu --&gt;
                  
                  &lt;li&gt;&lt;a href=&quot;index.php&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-tasks&quot;&gt;&lt;/i&gt;App Users&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;halt_sponsors_senate.php&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-tasks&quot;&gt;&lt;/i&gt;Senate HALT Supporters&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;halt_sponsors_assembly.php&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-tasks&quot;&gt;&lt;/i&gt;Assembly HALT Supporters&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;brief_call_history.php&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-tasks&quot;&gt;&lt;/i&gt;Call History&lt;/a&gt;&lt;/li&gt;                   
                  &lt;li&gt;&lt;a href=&quot;send_message.php&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-tasks&quot;&gt;&lt;/i&gt;Send Messages&lt;/a&gt;&lt;/li&gt;                   
                  &lt;!-- &lt;li&gt;&lt;a href=&quot;voice_prompt_form.php&quot;&gt;&lt;i class=class=&quot;glyphicon glyphicon-tasks&quot;&gt;&lt;/i&gt;Voice Prompt&lt;/a&gt;&lt;/li&gt; --&gt;
               
                  &lt;/li&gt;
              &lt;/ul&gt;
           &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;col-md-10&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
                  
                  &lt;div class=&quot;col-md-12&quot;&gt;
                      &lt;div class=&quot;content-box-large&quot;&gt;
                          &lt;div class=&quot;panel-body&quot;&gt;
                                  
                   
                              &lt;form class=&quot;pure-form pure-form-stacked&quot; method=&quot;POST&quot;&gt;
                                  &lt;fieldset&gt;
                                      &lt;legend&gt;Create Message&lt;/legend&gt;
      
                                      &lt;label for=&quot;title&quot;&gt;Title&lt;/label&gt;
                                      &lt;input type=&quot;text&quot; value=&quot;Support Halt&quot; id=&quot;title&quot; name=&quot;title&quot; class=&quot;pure-input-1-2&quot; placeholder=&quot;Enter title&quot;&gt;
                   
                                      &lt;label for=&quot;message&quot;&gt;Message&lt;/label&gt;
                                      &lt;textarea class=&quot;pure-input-1-2&quot; name=&quot;message&quot; id=&quot;message&quot; placeholder=&quot;Notification message!&quot; rows=&quot;5&quot; &gt;Hello World&lt;/textarea&gt;
                                      
                                      &lt;br&gt;
                                         &lt;h5&gt; 
                                          &lt;input type=&quot;radio&quot; id=&quot;user_category&quot; name=&quot;user_category&quot; value=&quot;userswithopponents&quot;&gt; Users with opponents&lt;br&gt;
                                          &lt;br&gt;
                                          &lt;input type=&quot;radio&quot; id=&quot;user_category&quot; name=&quot;user_category&quot; value=&quot;userswithsupporters&quot;&gt; Users with supporters&lt;br&gt;
                                          &lt;br&gt;
                                          &lt;input type=&quot;radio&quot; id=&quot;user_category&quot; name=&quot;user_category&quot; value=&quot;everyone&quot;&gt; Everyone &lt;br&gt;                                            
                                          &lt;br&gt;
                                          &lt;input type=&quot;radio&quot; id=&quot;user_category&quot; name=&quot;user_category&quot; value=&quot;nyresidentsonly&quot;&gt; NY residents only &lt;/h5&gt;
  
                                  &lt;/br&gt;
                   
                                     
                                      &lt;input type=&quot;hidden&quot; name=&quot;push_type&quot; value=&quot;individual&quot;/&gt;
                                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; onclick=&quot;sendMessage()&quot;&gt;Send&lt;/button&gt;
                                  &lt;/fieldset&gt;
                              &lt;/form&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
        &lt;/div&gt;
  
  
      &lt;/div&gt;
  &lt;/div&gt;
  
  
  
  &lt;link href=&quot;vendors/datatables/dataTables.bootstrap.css&quot; rel=&quot;stylesheet&quot; media=&quot;screen&quot;&gt;
  
  &lt;!-- jQuery (necessary for Bootstrap's JavaScript plugins) --&gt;
  &lt;script src=&quot;https://code.jquery.com/jquery.js&quot;&gt;&lt;/script&gt;
  &lt;!-- jQuery UI --&gt;
  &lt;script src=&quot;https://code.jquery.com/ui/1.10.3/jquery-ui.js&quot;&gt;&lt;/script&gt;
  &lt;!-- Include all compiled plugins (below), or include individual files as needed --&gt;
  &lt;script src=&quot;bootstrap/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  
  &lt;script src=&quot;vendors/datatables/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
  
  &lt;script src=&quot;vendors/datatables/dataTables.bootstrap.js&quot;&gt;&lt;/script&gt;
  
  &lt;script src=&quot;js/custom.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;js/tables.js&quot;&gt;&lt;/script&gt;
  </code></pre>
    
  
  <p>So, as of now I have tried to iterate over all reg_ids from database &amp; call send method but it didn't send notifications to any devices .</p>