Push notifications are a powerful marketing tool, a successful way to interact with consumers, and a way to increase app engagement. Since users don’t need to keep their apps open all the time to receive push notifications, app publishers can send them whenever they want. Push notifications can either appear as a pop-up. The user will be taken to the app when they tap or click the notification message.
We’ll look at two approaches to leveraging Firebase Cloud Messaging (FCM) in a CodeIgniter application to push notifications to a mobile app. We’ll go through two techniques: using cURL and the file_get_contents() function. Both approaches can be used to send notifications to the app.
Using cURL
Let’s consider the below example:
function sendPushNotification($fcmToken, $title, $body) {
$serverKey = 'YOUR_SERVER_KEY';
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . $serverKey,
'Content-Type: application/json'
);
$notification = array(
'title' => $title,
'body' => $body,
'click_action' => 'YOUR_CLICK_ACTION', // Replace with the action you want to perform when the user clicks the notification
'icon' => 'YOUR_ICON_URL' // Replace with the URL of the notification icon
);
$data = array(
'key1' => 'value1', // You can include additional data in the notification payload
'key2' => 'value2'
);
$fields = array(
'to' => $fcmToken,
'notification' => $notification,
'data' => $data
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
From the above code, sendPushNotification() is used to send a push notification to a specific device using Firebase Cloud Messaging (FCM) service. Replace the server key with your own FCM server key. And also customize the notification body and title.
Using file_get_contents()
function sendPushNotification($fcmToken, $title, $body) {
$serverKey = 'YOUR_SERVER_KEY';
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . $serverKey,
'Content-Type: application/json'
);
$notification = array(
'title' => $title,
'body' => $body,
'click_action' => 'YOUR_CLICK_ACTION', // Replace with the action you want to perform when the user clicks the notification
'icon' => 'YOUR_ICON_URL' // Replace with the URL of the notification icon
);
$data = array(
'key1' => 'value1', // You can include additional data in the notification payload
'key2' => 'value2'
);
$fields = array(
'to' => $fcmToken,
'notification' => $notification,
'data' => $data
);
$options = array(
'http' => array(
'header' => implode("\r\n", $headers),
'method' => 'POST',
'content' => json_encode($fields)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return json_decode($result, true);
}
You can send a POST request to the Firebase Cloud Messaging (FCM) API using the `stream_context_create` function and PHP’s built-in `file_get_contents` function. This technique can send push notifications as an alternative to cURL. Replace the server key with your own FCM server key and customize the notification content.
In conclusion, when comparing the two approaches, the decision will depend on the particular needs of the CodeIgniter application. The file_get_contents() is suitable for simple and easy implementation. When it comes to request handling and parameters, cURL is a better option for developers.
Sreyas IT Solutions is the best solution for all the CodeIgniter applications services and support. We provide other services like web, mobile application development, and e-commerce development and designing. Our client satisfaction is our prime motto.