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 Pakainfo.com – 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.

Read Also:  mysql delete duplicate rows - 3 ways to Delete Duplicate Rows in MySQL

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
CREATE TABLE IF NOT EXISTS `all_clients` (
  `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`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

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

//JYLIVEdhfjhdjKI8756sSdsfsWWsdsdsd
//
//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 Pakainfo.com Create database connection
mysql_connect("localhost","ar_admin","[email protected]");
mysql_select_db("livedbname");
 
//Devloped by Pakainfo.com 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 Pakainfo.com 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 Pakainfo.com Include FCM library file and create FCM library class object
require("fcm.php");
 
$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'];//Pakainfo.com Message which you want to send
 
//Devloped by Pakainfo.com Send Notification
$fcm->sendNotification($configDataArr);

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

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

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

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

        //Devloped by Pakainfo.com 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 Pakainfo.com Execute post
        $api_res = curl_exec($live_ch);
        if ($api_res === FALSE) {
            die('Curl failed: ' . curl_error($live_ch));
        }
        //Devloped by Pakainfo.com Close connection
        curl_close($live_ch);    
 
        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.

Read Also:  SQL left outer join Examples

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

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(Pakainfo.com) readers.Your Valuable Feed Back,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.