PHP Firebase Push Notification

In this tutorial I explain how to integrate PHP Firebase Push notification for both of websites & android mobile application. Most of the time notifications are remind us what we have to done in future and also remind the current updates. So every website owners & app users are looking for regular updates about the particular services. For example if you are the admin of Gold Jewellery shop ? then you have to delivered the daily gold & silver prices for your customers.

It’s like one of the under push notification using Firebase database & PHP programming languages. Through the feature we can able to send offer deals, price updates, holiday, emergency news, trending info and more. That’s why most of clients are asking this into developers. Therefore in this exercise I clearly explain how we have integrate our PHP code in Google Firebase database.

php firebase push notification

The initial step is we have to manually store the both prices, hereafter assign access permission via back-end language of PHP code. When we do this operation, on the other side store admin easily update the daily price for both of Gold & Silver rates. Alternatively you can also do this via other databases like MySQL, Oracle, MS SQL server etc. But here when we do via Firebase we can able to send notification for active users. This feature not available on other database providers. This is the major reason why everyone select Google Firebase to send the push notification to users.

Create Project – PHP Firebase Push Notification

Okay let’s see the steps for how send Firebase Cloud Messaging push notification with help of Firebase console. This is most searched query by software developers and college students. Behind the reason is already I was shared on introduction. Moreover some developers also search like send push notification without Firebase console features.

So here we see both steps and solution with real time examples. Suppose if you are beginner then once read Firebase tutorial which is developed specially for beginners. After reading those articles you get clear idea about how handle Firebase database and Firestore.

Documentation

We can send message for both of Web application and mobile apps using Firebase Notification composer. For the clear reference once checkout official steps for setup all things. Actually Firebase is a Cloud Messaging Server Side API which is used to send message for particular web or mobile apps. The FCM has totally two components and it’s explained in below section.

  • FCM Back-end by Google
  • Server Cloud Function by Google

Add & Initialize SDK

First you have to install the Firebase JS SDK for accessing further communication with Google. So initially we have to add SDK on our project directories.

import { initializeApp } from "firebase/app";
import { getMessaging } from "firebase/messaging";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// Initialize Firebase Cloud Messaging and get a reference to the service
const messaging = getMessaging(app);

The next step is you have to generate the Key pair for integrate with our back-end to web application services. After that we can easily able to end notification for client side end.

import { getMessaging, getToken } from "firebase/messaging";

const messaging = getMessaging();
// Add the public key generated from the console here.
getToken(messaging, {vapidKey: "BKagOny0KF_2pCJQ3m....moL0ewzQ8rZu"});

Next step is add functionality for receive messages from customer side. Upto 5GB spaces are available on starting period. In future surely we have to buy the premium plan for accessing and store more products.

installation

Install composer using following below command

composer require kerox/fcm

Source Code – PHP Firebase Push Notification

I hope above all steps are helps to how send Firebase notification into Google section. so once check again the application was sort out properly without any struggles. Most of developers are initially facing lot of issues when send and receive messages on cloud services.

<?php

$API_KEY ="[API KEY AQUI]";
$url = 'https://fcm.googleapis.com/fcm/send';
$notification = [
	'title' => 'Title',
	'body' => 'body of the notification',
];
$fields = array (
	"to" =>
	'[DESTINATION]',
	'notification'=> $notification,
	'data'=> $notification,
	'vibrate'=>'1',
	'sound'=>'default',
);
$fields = json_encode($fields);
$headers = array(
	'Authorization: key=' . $API_KEY,
	'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, $fields);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>

Leave a Reply