In this tutorial you will learn about the Laravel 9 Send FCM Push Notification using Firebase and its application with practical example.
In this Laravel 9 Send FCM Push Notification using Firebase Tutorial, I’ll show you how to send fcm push notification to android and Ios using firebase in laravel 9. In this tutorial you will learn send push notification using firebase in laravel 9. In this example we will create a custom helper function to send firebase push notification in laravel 9. In this article I will share example to send push notification in laravel 9 application.
Laravel 9 Send FCM Push Notification using Firebase Tutorial
In this step by step guide I’ll demonstrate you the process to send firebase push notification in laravel 9. Please follow the instruction given below:
- Create a Helper File
- Add Fcm Key in .env
- Send Push Notification.
Create a Helper File
In this step we will create a helper file. Go to App directory and inside this directory create a new file new helpers.php. Now put the following code into your helpers.php file as follow:
app/helpers.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php function send_notification_FCM($notification_id, $title, $message, $id,$type) { $accesstoken = env('FCM_KEY'); $URL = 'https://fcm.googleapis.com/fcm/send'; $post_data = '{ "to" : "' . $notification_id . '", "data" : { "body" : "", "title" : "' . $title . '", "type" : "' . $type . '", "id" : "' . $id . '", "message" : "' . $message . '", }, "notification" : { "body" : "' . $message . '", "title" : "' . $title . '", "type" : "' . $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; } //curl_close($crl); //print_r($result_noti);die; return $result_noti; } |
Add Fcm Key in .env
In this step we will add fcm key in laravel project’s .env file. Go to your project root directory and open .env file. Then update fcm key as follow:
1 |
FCM_KEY = |
Now, you can use your google fcm key as follow
1 2 |
1 $accesstoken = env('FCM_KEY'); |
Send fcm Push Notification
In this step, open your controller file and you can call send_notification_FCM($notification_id, $title, $message, $id,$type) from helpers.php with following parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public function notifyUser(Request $request){ $user = User::where('id', $request->id)->first(); $notification_id = $user->notification_id; $title = "Greeting Notification"; $message = "Have good day!"; $id = $user->id; $type = "basic"; $res = send_notification_FCM($notification_id, $title, $message, $id,$type); if($res == 1){ // success code }else{ // fail code } } |
send_notification_FCM() with following parameter $notification_id, $title, $message, $id,$type. Now when you want to send push notification you need to call the send_notification_FCM() with parameters as specified.