Send Push Notification to Users Using Firebase Messaging Service in PHP

Send Push Notification to Users Using Firebase Messaging Service in PHP

In this post we will indicate you Send Push Notification to Users Using Firebase Messaging Service in PHP, hear for Send Push Notification to Users Using Firebase Messaging Service in PHP we will give you demo and case for implement.

we will demonstrate to you the best approach to send boundless free push notices to your buyers exploitation base of operations Cloud electronic correspondence (FCM) in your PHP net application. Push Notifications ar interactive messages that returned from a site. they’re acclimated indicate warnings outside the online page setting but the client isn’t perusing the page he marked to. you’ll tests of push warning in a few far-celebrated around the world websites, Facebook and youtube.

Send Push Notification to Users Using Firebase Messaging Service in PHP
Send Push Notification to Users Using Firebase Messaging Service in PHP

What is FCM?

The FCM or firebase informing Service is that the new release of GCM (Google Cloud Messaging). It acquires the dependable and climbable GCM foundation, and new choices. base of operations Cloud {messaging|electronic messaging|electronic communication} (FCM) could be a cross-stage informing determination that empowers you to reliably convey messages at no esteem. Utilizing FCM, you’ll appreciate a customer application that new email or diverse information is available to synchronize.

Firebase Messaging Services

Browser Support

// Send Push Notification to Users Using Firebase Messaging Service in PHP
Chrome: 50+
Firefox: 44+
Opera Mobile: 37+

Note: Sending messages to the Notifications Console is not upheld.

Working

The Involves primarily 2

Get FCM Token From User

Show Notification To User

Before we keep an eye on Start

We have to frame a base venture by going to following connection. The techniques territory unit genuinely simple and undemanding along these lines i’m skipping it.

Let’s start Coding

1) Get FCM Token From User

// Send Push Notification to Users Using Firebase Messaging Service in PHP
<!-- add js file -->
<script src="https://www.gstatic.com/firebasejs/3.7.2/firebase.js"></script>
<script>
// for Initialize Firebase
var configs = {
apiKey: "add your api key",
authDomain: "addyour auth domain",
databaseURL: "add your database url",
storageBucket: "add your storage bucket",
messagingSenderId: "add your messaging id"
};
firebase.initializeApp(configs);

const messaging = firebase.messaging();

messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
return messaging.getToken();
})
.then(function(token) {
console.log(token); // hear Display user token
})
.catch(function(err) { // hear Happen if user deney permission
console.log('Unable to get permission to notify.', err);
});

messaging.onMessage(function(payload){
console.log('onMessage',payload);
})

</script>

we can get the customer token by opening the comfort. The onMessage work is utilized in light of the fact that we don’t have to send warning if the client is on your site page.

In genuine application, we have to send the token to your server by means of Ajax Post like underneath and store it in a table for sometime later.

// Send Push Notification to Users Using Firebase Messaging Service in PHP
$.ajax({
type:'POST',
url:'/storetoken',
data:{token : token, _token: "<?php echo csrf_token(); ?>"},
success:function(data){
$("#msg").html(data);
}
});

Note You have to make firebase-informing sw.js record and place it in your www or htdocs envelope before you execute the index.html document. The codes for it is given underneath.

// Send Push Notification to Users Using Firebase Messaging Service in PHP
importScripts('https://www.gstatic.com/firebasejs/3.7.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.7.1/firebase-messaging.js');

// for Initialize Firebase
var configs = {
apiKey: "add your api key",
authDomain: "add your auth domain",
databaseURL: "add your database url",
storageBucket: "add your storage bucket",
messagingSenderId: "add your messaging id"
};
firebase.initializeApp(configs);

const messaging = firebase.messaging();

Note: You likewise need to spare your symbol in www or htdocs envelope.

2) Send Notification To User

Again two discretionary techniques

a) By utilizing twist in summon line

On the off chance that you are utilizing windows you can download twist for 64 bit from following the connection. In the wake of downloading it unfasten it and duplicate the substance inside receptacle organizer to a recently made twist envelope inside your c drive and add that envelope to windows way.

In the event that you are utilizing Linux a basic well-suited get introduce twist summon is sufficient to introduce twist.

Presently we have to post client id data and notice data in JSON to https://fcm.googleapis.com/fcm/send alongside verification enter in the header.

You can get confirmation key or API_ACCESS_KEY by going to extend setting could informing segment in firebase comfort.

The code for twist charge line is given beneath

curl -X POST --header "Authorization: key=ADD-API-ACCESS-KEY-HEAR" \ --Header "Content-Type: application/json" \ https://fcm.googleapis.com/fcm/send \ -d "{\"to\":\"cNf2Sx----9\",\"notification\":{\"title\" : \"onlinecode\",\"body\":\"A Code Sharing Blog!\",\"icon\": \"icon.png\",\"click_action\": \"http://onlinecode\"}}"

The specimen yield picture is given beneath

FCM Curl Sample Image –

b) By calling twist by utilizing PHP scripts (Recommended and simple method)

The code for curl.php record is given beneath

<?php
// Send Push Notification to Users Using Firebase Messaging Service in PHP
// Server key from Firebase Console
define( 'API_ACCESS_KEY', 'ADD-API-ACCESS-KEY-HEAR' );

$data = array("to" => "cNf2---6Vs9",
"notification" => array( "title" => "Shareurcodes.com", "body" => "A Code Sharing Blog!","icon" => "icon.png", "click_action" => "http://onlinecode"));
$data_string = json_encode($data);

echo "Json Data : ".$data_string;

$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);

$ch = curl_init();

curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, $data_string);

$result = curl_exec($ch);

curl_close ($ch);

echo "<p>&nbsp;</p>";
echo "The Result : ".$result;

Note you have to execute curl.php document utilizing another program ie not from the program that is utilized to get the client token. You can see notice just on the off chance that you are perusing another site.

Now what?

Note: The FCM SDK is bolstered just in pages served over HTTPS. This is because of its utilization of administration laborers, which are accessible just on HTTPS destinations.

On the off chance that you are making true application then the initial step is purchasing SSL testament to your server.

Store client token data in your server. Send the message by calling a cul work one by one for every client. The change it as indicated by your need. The above code gives fundamental center pieces. Remark beneath on the off chance that you have any proposals or questions.

You can demo this application by going by following connection.

DEMO

Reference @ shareurcodes.com

2 thoughts on “Send Push Notification to Users Using Firebase Messaging Service in PHP”

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  57  =  58

We accept paid advance and paid guest Posting on our Site : Contact US