YouTube Icon

How to Send Push Notification to Android Using PHP And Firebase




How to Send Push Notification to Android Using PHP And Firebase

Fundamentally, we have utilized GCM(Google Cloud Messaging) for sending pop-up message to Android gadgets

As of late Google has been dispatched Firebase Cloud Messaging (FCM), the most recent variant of GCM (Google Cloud Messaging) with more properties. 

This article is helpful for the PHP engineers who need to send pop-up messages by utilizing PHP with FCM (Firebase Cloud Messaging). 

Message pop-up was a viral convention which offers sprout to business in Today's computerized world. Here we will disclose to you about, What is Push Notification? How to make a Push Notification utilizing various stages/administrations like PHP (Hypertext Preprocessor ), GCM (Google Cloud Messaging) and FCM (Firebase Cloud Messaging)? 

Also Read:- How to Implement Real Time Notification Using Socket.io and Node.JS?

What is Push Notification? 

When all is said in done, Push warnings are the message/Notifications you will get in Android. This causes the clients to draw in with the customers on advanced stages. This administration can be started in whichever way structure clients or from customers. This has the fundamental effect and prompts regions of convention into two structures. The first is User enlistment and the following one is Client broadcasting. 

In User enlistment, the client will send the solicitation for the administration from the customers, through GCM/FCM in an informing design. At that point the cloud worker will start a token with the novel data to interface the client with the customer. This symbolic id of the client will be put away in a distant information base. At that point the association between the client and customer will be open. 

In Client Broadcasting, the customer will raise the solicitation for the outer client. This solicitation will have a novel Authorization key and Device token for a rundown of clients. The cloud administration ( FCM/GCM ) acknowledges the solicitation and broadcasts the message to all clients on the rundown. The client will get a notice on Android App. 

Also Read:- How to Put a Calculator in the Mac Notifications Center

You can send message pop-up to android utilizing php. here are the means, lets, look at it. 

How to Send a Push Notification Using PHP? 

Essential: 

  • Approval key (Server Key) 
  • Device Token 

You can get gadget token at the hour of enrolling by the android clients utilizing API. 

Lets Create PHP Files 

  • Goto your WAMP/XAMPP registry and make another envelope named warning inside htdocs or www. 
  • Make a record named fcm.php and add the beneath pop-up message php code. Here we characterize the Firebase Server API Key to send a solicitation to firebase
<?php
class FCM {

  	public function send_notification($registatoin_ids, $notification,$device_type) {
		$url = 'https://fcm.googleapis.com/fcm/send';
		if($device_type == "Android"){
		    $fields = array(
		        'to' => $registatoin_ids,
		        'data' => $notification
		    );
		} else {
		    $fields = array(
		        'to' => $registatoin_ids,
		        'notification' => $notification
		    );
		}
		$headers = array('Authorization:key=Your Firebase Server API Key Here','Content-Type:application/json');
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		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);
		if ($result === FALSE) {
		  die('Curl failed: ' . curl_error($ch));
		}
		curl_close($ch);
  	}
}   
?>

Also Read:- PayPal Standard Payment Gateway Integration in PHP

Make certain to supplant the Your Firebase Server API Key Here with an appropriate one from the Google API's Console page

  • Finally, create an index.php and add the below code. 
<?php
$regId ="Your Device Token";
include_once 'fcm.php';     
$notification = array();
$arrNotification= array();			
$arrData = array();											
$arrNotification["body"] ="Its testing notification.";
$arrNotification["title"] = "Notification";
$arrNotification["sound"] = "default";
$arrNotification["type"] = 1; 
$fcm = new FCM();
$result = $fcm->send_notification($regId, $arrNotification,"Android");
?>

Prior to testing please supplant Your Device Token with your genuine gadget token. 

Send Push Notification to IOS Using PHP 

Also Read:- PayPal Standard Payment Gateway Integration in PHP

You simply need to change "Android" to "IOS" in your index.php record. like beneath… 

$result=$fcm->send_notification($regId,$arrNotification,"Android"); 

To 

$result=$fcm->send_notification($regId, $arrNotification,"IOS"); 

Expectation it caused you in a simple manner to send message pop-up to android utilizing php. If you don't mind let us know in the remark segment in the event that you have any worry. 

You can likewise impart to your companions, who need to send message pop-up to android and IOS utilizing PHP.

Also Read:- How to Set Up iTunes Match on All Your Computers and iOS Devices



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink

Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala
CFT

News & Blogs

58de4563a561421b9d27a5f44972b306.jpg

Top Mobile App Development Trends You Absolutel...

While none of us could have predicted there would be a global pandemic in early 2020, experiencin...

cf72de50fea16ff9b8476b6ecb323b2c.jpg

Android apps which very help full in our daily ...

There are lots of android apps which very help full in our daily life. We stay in an era&nb...

0d13c713f0b37875163f3fc23b24b150.jpg

How To Upload An App To Google Play Store?

Nobody can question the prevalence and strength of the Google Play Store in the area of versatile...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh
CFT

Our Client Says

WhatsApp Chat with Our Support Team