Fetch Multiple Device Tokens From Db and Send FCm using PHP

Viewed 674

I have to fetch device Tokens from Db and Send it to Multiple Device Now i have Query to fetch multiple device Token from DB

$queryname1 = "SELECT device_id FROM  register where id IN ($particpants)";
    $resultname1 = mysql_query($queryname1) or die('Errant query:  ' . $queryname1);
          $idarray = array();
               while ($idrows = mysql_fetch_assoc($resultname1))
              {
                  $idarray = $idrows['device_id'];
              }

And My FCm Code is below i am not able to send fcm Notification here us my code

    define("API_ACCESS_KEY", "********"); 
$registrationIds=$idarray;
         $msg = array
              (
            'body'  => 'Body  Of Notification',
            'title' => 'Title Of Notification',
                    'icon'  => 'myicon',/*Default Icon*/
                    'sound' => 'mySound'/*Default sound*/
              );
        $fields = array
                (
                    'to'        => $registrationIds,
                    'notification'  => $msg
                );
                //print_r($fields);
                //exit;

        //'registration_ids'  => array($registrationIDs),
        $headers = array
                (
                    'Authorization: key=' . API_ACCESS_KEY,
                    'Content-Type: application/json'
                );
    #Send Reponse To FireBase Server    
            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
            curl_setopt( $ch,CURLOPT_POST, true );
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            $result = curl_exec($ch );
            curl_close( $ch );

            echo $result;
1 Answers
Related