PHP Send Push Notification To Android & IOS using Google FCM Example

Send Push Notification to Android Using PHP And Firebase

Basically, we have used GCM(Google Cloud Messaging) for sending push notification to Android devices.
Recently Google has been launched Firebase Cloud Messaging (FCM), the latest version of GCM (Google Cloud Messaging) with more properties.

This article is useful for the PHP developers who want to send push notifications by using PHP with FCM(Firebase Cloud Messaging).

Push notification was a viral protocol which gives bloom to business in Today’s digital world. Here we will explain to you about, What is Push Notification? How to create a Push Notification using different platforms/services like PHP (Hypertext Preprocessor ), GCM (Google Cloud Messaging) and FCM (Firebase Cloud Messaging)? Let’s start

What is Push Notification?

In general, Push notifications are the message / Notifications you will receive in Android. This makes the customers engage with the clients on digital platforms. This service can be initiated in either way form users or from clients. This makes the basic difference and leads to subdivisions of protocol into two forms. The first one is User registration and the next one is Client broadcasting.

In User registration, the user will send the request for the service from the clients, through GCM/FCM in a messaging format. Then the cloud server will initiate a token with the unique information to connect the user with the client. This token id of the user will be stored in a remote database. Then the connection between the user and client will be open.

In Client Broadcasting, the client will raise the request for the external user. This request will have a unique Authorization key and Device token for a list of users. The cloud service ( FCM/GCM ) accepts the request and broadcasts the message to all users on the list. The user will get a notification on Android App.

You can send push notification to android using php. here are the steps, lets, check it out.

Читайте также:  Div class box html

How to Send a Push Notification Using PHP?

  1. Authorization key (Server Key)
  2. Device Token
    You can get device token at the time of registering by the android users using API.

Lets Create PHP Files

  1. Goto your WAMP/XAMPP directory and create a new folder named notification inside htdocs or www.
  2. Create a file named fcm.php and add the below push notification php code. Here we define the Firebase Server API Key to send a request to firebase.

Источник

Firebase send push php

Today I will show you how to send unlimited free push notifications to your clients using Firebase Cloud Messaging (FCM) in your PHP web application. Push Notifications are clickable messages that come from a website. They are used to show notifications outside the web page context even if the user is not browsing the page he subscribed to. You can examples of push notification in many famous blogs, Facebook and youtube.

What is FCM?

The FCM or Firebase Messaging Service is the new version of GCM (Google Cloud Messaging). It inherits the reliable and scalable GCM infrastructure, plus new features. Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost. Using FCM, you can notify a client app that new email or other data is available to sync.

Firebase Messaging Services

Browser Support

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

Before We Start

We need to make a firebase project by visiting following link. The procedures are fairly simple and straight forward so I am skipping it.

Let’s Start Coding

1) Get FCM Token From User

Get the web setup codes from firebase console by visiting Authentication tab. The codes for index.html file is given below

  

You can get the client token by opening the console. The onMessage function is used because we don’t need to send notification if the user is on your web page.

In real world application, you need to send the token to your server via Ajax Post like below and store it in a table for future use.

Note You need to create firebase-messaging-sw.js file and put it in your www or htdocs folder before you execute the index.html file. The codes for it is given below.

importScripts('https://www.gstatic.com/firebasejs/3.7.1/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/3.7.1/firebase-messaging.js'); // Initialize Firebase var config = < apiKey: "your api key", authDomain: "your auth domain", databaseURL: "your database url", storageBucket: "your storage bucket", messagingSenderId: "your messaging id" >; firebase.initializeApp(config); const messaging = firebase.messaging();

Note: You also need to save your icon in www or htdocs folder.

Читайте также:  Php check if null or false

2) Send Notification To User

Again two optional methods

a) By using curl in command line

If you are using windows you can download curl for 64 bit from following the link. After downloading it unzip it and copy the contents inside bin folder to a newly created curl folder inside your c drive and add that folder to windows path.

If you are using Linux a simple apt-get install curl command is enough to install curl.

Now we need to post user id information and notification information in JSON to https://fcm.googleapis.com/fcm/send along with authentication key in the header.

You can get authentication key or API_ACCESS_KEY by visiting project setting could messaging section in firebase console.

The code for curl command line is given below

curl -X POST --header "Authorization: key=AAAA-----FE6F" \ --Header "Content-Type: application/json" \ https://fcm.googleapis.com/fcm/send \ -d ">" 

The sample output image is given below

FCM Curl Sample Image

b) By calling curl by using PHP scripts (Recommended and easy method)

The code for curl.php file is given below

 "cNf2---6Vs9", "notification" => array( "title" => "Shareurcodes.com", "body" => "A Code Sharing Blog!","icon" => "icon.png", "click_action" => "http://shareurcodes.com")); $data_string = json_encode($data); echo "The 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 "

 

"; echo "The Result : ".$result;

Note you need to execute curl.php file using another browser ie not from the browser that is used to get the user token. You can see notification only if you are browsing another website.

Note: The FCM SDK is supported only in pages served over HTTPS. This is due to its use of service workers, which are available only on HTTPS sites.

If you are making real world application then the first step is buying SSL certificate to your server.

Store user token information in your server. Send the message by calling a cul function one by one for each user. The modify it according to your need. The above code gives basic core snippets. Comment below if you have any suggestions or doubts.

Читайте также:  Wordpress admin ajax php отключить

DEMO

You can demo this application by visiting following link.

Источник

PHP Send Push Notification To Android & IOS using Google FCM Firebase

Send push notification to android and ios using PHP + FCM firebase. In this tutorial, you will learn how to send push notifications to android and ios using PHP FCM firebase.

This tutorial will explain each thing is a very simple and easy example for send push notifications to android in php using google firebase fcm.

Send Push Notification to Android and IOS using PHP + FCM Firebase

  • Step 1 – Create PHP Files
  • Step 2 – Create fcm.php File
  • Step 3 – Create send-notification.php File
  • Step 4 – Run Application

Step 1 – Create Index.php File

In this step, create a php file which named index.php. And then add the following code into it:

      
Device Type
Notification Id
Submit

Step 2 – Create fcm.php File

In this step, create a file named fcm.php and add the following code for push notification into it:

Note that, Please make sure to define the Firebase Server API Key to send a request to firebase.

 /** * Sending Push Notification */ 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 ); > // Firebase API Key $headers = array('Authorization:key=Your Firebase Server API Key Here','Content-Type:application/json'); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Disabling SSL Certificate support temporarly 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); > > ?>

Step 3 – Create send-notification.php File

In this step, create send-notification.php file and add the following code into it:

send_notification($regId, $arrNotification,$dType); print_r($result); > ?>

Step 4 – Run Application

In this step, open your terminal and execute the following command to quick run this application:

cd project_directory php -S localhost:8000

Now you can open the following URL on your browser:

Conclusion

In this tutorial, you have learned how to send push notification to ios and android using php fcm.

Источник

Оцените статью