Send Push Notification to Android and IOS In Codeigniter 4

20-Apr-2022

.

Admin

Send Push Notification to Android and IOS In Codeigniter 4

Hi Guys,

If you need to see an example of sending a push notification to android using Codeigniter 4. it's a simple example of sending a notification in Codeigniter 4. We will look at an example of how to send push notifications in Codeigniter 4. In this article, we will implement a Codeigniter 4 to send push notifications to android and ios examples. Let's see below example push notification Codeigniter 4 tutorial.

codeigniter 4 send push notification to android and ios cell using firebase fcm. on 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


This is optional; however, 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, 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

Further, you need to generate a new controller that manages the online stripe transaction, hence create a Notification.php file and append the example code in..

app/Controllers/Notification.php

namespace App\Controllers;

use CodeIgniter\Controller;

class Notification extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

return view('index');

}

/**

* Write code on Method

*

* @return response()

*/

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');

$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 index file. Likewise, open and add the suggested code example in application/views/index.php file:

application/views/index.php

<!DOCTYPE html>

<html>

<head>

<title>Codeigniter 4 Send Push Notification using Google FCM Example - Nicesnippets.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