Codeigniter 4 - Send Push Notification to Android and IOS Example

Apr 20, 2022 . Admin



Hi Friends,

This tutorial will give you an example of sending push notifications to android using Codeigniter 4. you can see send notification in Codeigniter 4. if you have questions about how to send a push notification in Codeigniter 4 then I will give a simple example with a solution. step by step explain CodeIgniter 4 send push notifications to android and ios example.

If you need to see an example of sending push notifications to android using CodeIgniter 4. Codeigniter 4 send push notification to android and ios cell using firebase fcm. In this educational, you'll discover ways to ship push notifications to android and ios mobile using firebase fcm in Codeigniter 4 app.

So let's start to the example.

Step 1: Install Codeigniter 4

First of all if you have not created the codeigniter app, then you may go ahead and execute the below command:

composer create-project codeigniter4/appstarter ci-news

After Download successfully, extract clean new Codeigniter 4 application.

Step 2 : Basic Configurations

So in this step we will now set basic configuration on the app/config/app.php file, so let’s implement to application/config/config.php and open this file on text editor.

app/config/app.php
public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/example/';
Step 3 : Set Up Controller

Here in this step we will create new controller that manages the online stripe transaction, hence create a SendNotification.php file and append the example code in..

app/Controllers/SendNotification.php
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');
 
        $accesstoken = 'YOUR FCM KEY';
 
        $URL = 'https://fcm.googleapis.com/fcm/send';
 
            $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"
                },
 
            }';
 
        $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) {
            $result_noti = 0;
        } else {
            $result_noti = 1;
        }
 
        echo view('success');
    }
}
Step 4 : Set Up View

Head over to application/views/ folder, create a new home file. Likewise, open and add the suggested code example in application/views/home.php file:

application/views/home.php
<!DOCTYPE html>
<html>
<head>
    <title>Codeigniter 4 Send Push Notification using Google FCM Example - Mywebtuts.com</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<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>
</body>
</html>

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:

application/views/success.php
<!DOCTYPE html>
<html>
<head>
  <title> send push notification to android using codeigniter </title>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <h1 class="text-center"> Please check your device ! Thanks</h1>
    </div>
</body>
</html>
Step 5 : Run Codeigniter App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Codeigniter app:

php spark serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8080/

I hope it can help you...

#Codeigniter 4