FCM Push notification [ Server side] HTTP via PHP

35 sec read

Dear friends

we will be having php code that can make http post request via curl to Firebase Push servers.

this post request will be having few parameters

and now it will take all the data to Push servers.

So on behalf of your server Firebase Push servers will deliver message to intended recipient.

Simple right .

public function deliverMessagesToIntendedRecipients($recipientEmailId, $message) {




        $connectionObj = $this->processDBConnection();




        $url = 'https://fcm.googleapis.com/fcm/send';
        $ch = curl_init($url);

        $sql = "SELECT FCMID from CHAT_USER WHERE EMAILID='$recipientEmailId'";



        $i = 0;
        $toArray = array();
        foreach ($connectionObj->query($sql) as $row) {
            $toArray[$i++] = $row['FCMID'];
        }

        $toStr = "";

        $j = 0;
        foreach ($toArray as $str) {
            $toStr.=$str;
            if ($j < count($toArray) - 1) {
                $toStr.=",";
            }
            $j++;
        }






        $jsonData = array(
            'registration_ids' => $toArray,
            'data' => array("Message" => $message, "Message Type" => 'text', "time" => date('now'))
        );

        $jsonDataEncoded = json_encode($jsonData);

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization:key= to be retrived from Firebase Console Project'));

        $result = curl_exec($ch);
    }

Thanks