How to Send Push notification using FCM using php

How to Send Push notification using FCM using php

In this Post We Will Explain About is How to Send Push notification using FCM using php With Example and Demo.Welcome on – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to send push notification to android using php Example

In this post we will show you Best way to implement push notification in php website, hear for firebase cloud messaging server php with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

how to send image in push notification android using php

FCM stands for Fire base Cloud Messaging(FCM) is a provide google cloud simple service messaging service this is used for all the simple sending notification and clients messages in step by step cross-platform for any google android application, and web-application as well as ios application,here simple source code, which is free (Firebase Cloud Messaging) available at no any cost.

If We want to use this (Fire base Cloud Messaging) service in your web-application please just simple follow below simple legal steps available and then We can send (Fire base Cloud Messaging) notification using google FCM(Firebase Cloud Messaging) service.

create MySQL Database Table for Insert Clients device ids and store data in my sql table

Step 1 : Create simple database table in which we need to store all the Client’s device token.

//(Fire base Cloud Messaging)  Create Table Query
  `client_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `client_pass` varchar(255) NOT NULL,
  `mobile_number` varchar(12) NOT NULL,
  `live_fcm_device` text NOT NULL,
  `cr_date` datetime NOT NULL,
  PRIMARY KEY (`client_id`)

Step 2 : And then make database table We need to store Clients information in table with (Firebase Cloud Messaging) FCM device id.

//Clients Information
$name = "Krunal Shah";
$email = "[email protected]";
$client_pass = MD5("jaydeepgondaliya");
$mobile_number = "9898555557";
$live_fcm_device = "dsfdshjfhdsf565s:JYLIVEdhfjhdjKI8756sSdsfsWWsdsdsd-hdsjfgdsjfds_kdfshfhdfdsfdsdsf-jayddfhfnagdinjdhffflive25kshdrubhjsjsh4585555KKSSAAghhgggsbddufnAA";
$cr_date = date("Y-m-d h:i:s");

//Insert Clients information with FCM device id
$api_res = mysql_query("INSERT INTO all_clients(name, email, client_pass, mobile_number,live_fcm_device,cr_date) VALUES('$name', '$email', '$client_pass', '$mobile_number', '$live_fcm_device','$cr_date')");

PHP script Source for simple sending notification using FCM.

Step 1 : Get all database Clients FCM device id, on which We want to FCM send notifications.

//Devloped by Create database connection
//Devloped by Select query for get all Clients FCM device ids
$api_res = mysql_query("SELECT * FROM all_clients") or die(mysql_error());
$allDeviceArr= array();
//Devloped by Prepare device ids array
while($rowData = mysql_fetch_assoc($api_res)) {
    $allDeviceArr[] = $rowData['live_fcm_device'];

Step 2 : Create data array with device ids for sending push notification

//Devloped by Include FCM library file and create FCM library class object
$fcm = new Fcm();//Create Fcm class object
$configDataArr = array();
$configDataArr['device_id'] = $allDeviceArr;//fcm device ids generate array which is created previously
$configDataArr['message'] = $data['message'];// Message which you want to send
//Devloped by Send Notification

Step 3 : create and define simple Fcm class in PHP fcm.php file, which is simple included previously methods

//Devloped by Define Fcm class
class Fcm {
    //Devloped by Define SendNotification function
    function sendNotification($configDataArr) {
    	$fcmApiKey = 'PUT_YOUR_FCM_API_KEY';//App API Key(Devloped by It is google cloud messaging api key not web api key)
        $url = '';//Devloped by here 

    	$registrationIds = $configDataArr['device_id'];//Devloped by Fcm Device ids array

    	$message = $configDataArr['message'];//Devloped by Message which you want to send
        $title = $configDataArr['message'];

        //Devloped by prepare the bundle
        $msg = array('message' => $message,'title' => $title);
        $fields = array('registration_ids' => $registrationIds,'data' => $msg);

        $headers = array(
            'Authorization: key=' . $fcmApiKey,
            'Content-Type: application/json'
        $live_ch = curl_init();
        curl_setopt( $live_ch,CURLOPT_URL, $url );
        curl_setopt( $live_ch,CURLOPT_POST, true );
        curl_setopt( $live_ch,CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $live_ch,CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $live_ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $live_ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $api_res = curl_exec($live_ch );
        //Devloped by Execute post
        $api_res = curl_exec($live_ch);
        if ($api_res === FALSE) {
            die('Curl failed: ' . curl_error($live_ch));
        //Devloped by Close connection
        return $api_res;

In this Example,First of all Add or Inluce External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priorty set.After that Include your relavant CSS Class.

Example: send push notification to android php

To send a push notification to an Android device using PHP, you will need to use Google Cloud Messaging (GCM) or Firebase Cloud Messaging (FCM) API. Here’s an example of how you can use FCM API to send a push notification using PHP:

First, you need to create a Firebase account and create a new project. Once you have created the project, you need to generate a server key by going to the project settings and then to the Cloud Messaging tab. Copy the server key as you will need it in the next step.

Next, you need to include the Firebase PHP library in your project. You can install it using Composer or download it from GitHub. Here’s an example of how to install it using Composer:

composer require kreait/firebase-php

After including the Firebase PHP library, you can use the following code to send a push notification:


$messaging = $factory->createMessaging();

$message = CloudMessage::withTarget('token', 'your-device-token')
    ->withNotification(Notification::create('Title', 'Message'))
    ->withData(['key' => 'value']);

$result = $messaging->send($message);


In this example, you need to replace the following:

/path/to/your/service-account.json: The path to your Firebase service account JSON file. Your Firebase project URL.
your-device-token: The token for the Android device you want to send the notification to.

Once you run this code, the Android device will receive the push notification with the specified title and message.

Note that this is just a basic example, and you can customize the push notification further by adding more parameters to the CloudMessage object.

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me FeedBack.
More Details……
Angularjs Example


I hope you have Got What is how to send image in push notification android using php And how it works.I would Like to have Feed Back From My Blog( readers.Your Valuable Feed Back,Any Question,or any Comments about This Article( Are Most Always Welcome.

Leave a Comment