CodeIgniter 4 Send Push Notification to IOS Using Firebase

Share this Article
Reading Time: 7 minutes
347 Views

In general, a Push notifications are the messages / Notifications that we receive in devices. This really makes the customers/subscribers engage with the client’s content and their updates on digital platforms.

Inside this article, we will see the concept of CodeIgniter 4 Send Push Notification to IOS Using Firebase. Step by step guide to implement and use it.

Already we have an article of sending firebase push notification on Android in Laravel 8, Click here to go.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

This article assuming you have the concept of little bit of firebase and it’s console.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Create Firebase Project and App

First we need to create a project in Firebase console for web application notification. To create project Click here to go to console.

Creating a Firebase Project at Firebase console.

Step #1

Step #2

Step #3

Step #4

Back to Firebase Console dashboard panel. We can see we will have a setup icon for ios device as you can see in this given image. Simply click on that Icon and then the process of Setting up the registration of App will start.

Register a IOS App into Firebase Project.

After creating App from here, back to firebase console. Go to App settings by click on gear icon and Click on Cloud messaging tab. You will find the Server key, we need that.

So copy server key.

Successfully, we have now completed the whole process of Creating Firebase Project > Register IOS App > Copy & Paste Server API Key.


Create API Route

We need to create a POST request api route which will be hit by IOS device. Open up the file Routes.php from /app/Config and copy the given code into and paste it.

//..Other routes

$routes->post('send-notification', 'NotificationController::send');

Create Controller & Add Method

Open project into terminal and run this spark command to create.

$ php spark make:controller Notification --suffix

It will create NotificationController.php file at /app/Controllers folder.

Open NotificationController.php and write this code.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class NotificationController extends BaseController
{
	public function send()
	{
		$device_token = $this->request->getVar("device_token");

		return $this->sendNotification($device_token, array(
			"title" => "Sample Message",
			"body" => "This is Test message body"
		));
	}

	public function sendNotification($device_token, $message)
	{
		$SERVER_API_KEY = '<YOUR-SERVER-API-KEY>';

		// payload data, it will vary according to requirement
		$data = [
			"to" => $device_token, // for single device id
			"notification" => $message
		];
		$dataString = json_encode($data);

		$headers = [
			'Authorization: key=' . $SERVER_API_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_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);

		$response = curl_exec($ch);

        curl_close($ch);
      
		return $response;
	}
}

The above code will send notification to a single device ID which we will pass into request.

Let’s say that we want to send notifications to multiple devices.

So how will we send ?

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class NotificationController extends BaseController
{
	public function send()
	{
		$device_token1 = $this->request->getVar("device_token1");
		$device_token2 = $this->request->getVar("device_token2");
		$device_token3 = $this->request->getVar("device_token3");

		return $this->sendNotification(array(
			$device_token1,
			$device_token2,
			$device_token3
		), array(
			"title" => "Sample Message",
			"body" => "This is Test message body"
		));
	}

	public function sendNotification($device_tokens, $message)
	{
		$SERVER_API_KEY = '<YOUR-SERVER-API-KEY>';

		// payload data, it will vary according to requirement
		$data = [
			"registration_ids" => $device_tokens, // for multiple device ids
			"notification" => $message
		];
		$dataString = json_encode($data);

		$headers = [
			'Authorization: key=' . $SERVER_API_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_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);

		$response = curl_exec($ch);
      
        curl_close($ch);

		return $response;
	}
}

When we send for a single device, we need to use

$data = [
    "to" => $device_token, // for single device id
    "notification" => $message
];

And for multiple devices, we have the key as registration_ids. If we want use this registration_ids to send only to a single device, it also works. Only we need to pass a single device token.

$data = [
    "registration_ids" => $device_tokens, // for multiple device ids
    "notification" => $message
];

We hope this article helped you to learn about CodeIgniter 4 Send Push Notification to IOS Using Firebase in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment