Codeigniter 4 Send Push Notification to Android and IOS Example

CodeIgniter 4 send push notification to android and IOS mobile using firebase fcm. In this tutorial, you will learn how to send push notifications to android and IOS mobile using firebase fcm in CodeIgniter 4 app.

As well as, This tutorial will guide the PHP and CodeIgniter developers who want to send FCM(Firebase Cloud Messaging) notifications by using PHP Codeigniter.

Send Push Notification to Android and IOS In Codeigniter 4 App

  • Download Codeigniter Latest
  • Basic Configurations
  • Create Controller
  • Create Views
  • Start Development server

Step 1 – Download Codeigniter Project

In this step, you will download the latest version of Codeigniter 4, Go to this link Download Codeigniter 4 fresh new setup and unzip the setup in your local system xampp/htdocs/ . And change the download folder name “demo”

Step 2 – Basic Configurations

In this step, you will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

public $baseURL = 'http://localhost:8080';
public $baseURL = 'http://localhost/demo/';

Step 3 – Create Controller

In this step, visit to app/Controllers and create a controller name Notification.php. Then add the following code into it:

<?php namespace App\Controllers; use CodeIgniter\Controller; class Notification extends Controller
{ public function index() { return view('index'); } public function sendPushNotification() { $val = $this->validate([ 'nId' => 'required', ]); $notification_id = $this->request->getVar('nId'); $title = 'Demo Notification'; $message = 'First codeigniter notification for mobile'; $d_type = $this->request->getVar('device_type'); // for android or IOS $accesstoken = 'YOUR FCM KEY'; $URL = ''; $post_data = '{ "to" : "' . $notification_id . '", "data" : { "body" : "", "title" : "' . $title . '", "type" : "' . $d_type . '", "id" : "' . $id . '", "message" : "' . $message . '", }, "notification" : { "body" : "' . $message . '", "title" : "' . $title . '", "type" : "' . $d_type . '", "id" : "' . $id . '", "message" : "' . $message . '", "icon" : "new", "sound" : "default" }, }'; // print_r($post_data);die; $crl = curl_init(); $headr = array(); $headr[] = 'Content-type: application/json'; $headr[] = 'Authorization: ' . $accesstoken; curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($crl, CURLOPT_URL, $URL); curl_setopt($crl, CURLOPT_HTTPHEADER, $headr); curl_setopt($crl, CURLOPT_POST, true); curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); $rest = curl_exec($crl); if ($rest === false) { // throw new Exception('Curl error: ' . curl_error($crl)); //print_r('Curl error: ' . curl_error($crl)); $result_noti = 0; } else { $result_noti = 1; } echo view('success'); }

Step 4 – Create Views

In this step, you need to create an index.php file. So visit application/views/ folder and create index.php file. and add the following HTML into your files:.

<!DOCTYPE html>
<head> <title>Codeigniter 4 Send Push Notification using Google FCM Example</title> <link rel="stylesheet" href="">
<body> <div class="container"> <br> <?= \Config\Services::validation()->listErrors(); ?> <div class="row"> <div class="col-md-9"> <form action="<?php echo base_url('public/index.php/notification/sendPushNotification') ?>" method="post" accept-charset="utf-8"> <div class="form-group"> <label for="formGroupExampleInput">Device Type</label> <select class="form-control" id="device_type" name="device_type" required=""> <option value="">Select Device type</option> <option value="android">Android</option> <option value="iphone">IOS</option> </select> </div> <div class="form-group"> <label for="formGroupExampleInput">Notification Id</label> <input type="text" name="nId" class="form-control" id="formGroupExampleInput" placeholder="Please enter notification id" required=""> </div> <div class="form-group"> <button type="submit" id="send_form" class="btn btn-success">Submit</button> </div> </form> </div> </div> </div>

This below line display error messages on your web page:

<?= \Config\Services::validation()->listErrors(); ?>

Now you need to create success.php file, so go to application/views/ and create success.php file. And put the below code into it:

<!DOCTYPE html>
<head> <title> send push notification to android using codeigniter </title> <link rel="stylesheet" href="">
<body> <div class="container mt-5"> <h1 class="text-center"> Please check your device ! Thanks</h1> </div>

Step 5 – Start Development Server

In this step, open your terminal and execute the following command to start development sever:

php spark serve

Then, Go to the browser and hit below the URL:



Send push notification to android using PHP CodeIgniter fcm, you have learned how to send push notifications to android and iPhone mobile using firebase fcm in CodeIgniter 4 app.

Recommended CodeIgniter Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

Related Posts