How to send push notification to mobile using Laravel - PHP using Firebase
In this article we will learn how to send push notification to Mobile or any other devices in Laravel or PHP . For sending push notification to devices we will use Google's firebase as a middleware . In this article i will take an example where i will send push notification to mobile device using firebase .
Google's firebase is an open source platform , so you don't need to pay any amount for using this service for sending push notification using firebase . The example i have shown below to send push notification using firebase can be used in Laravel 6,Laravel 7 and Laravel 8 versions .
Steps :-
- Create a Google Firebase account
- Get the server key
- Create Laravel Project
- Setup Route and Controller
- Code to send Notification
- Expected Errors and Solution
Step 1 - Create a Google Firebase account
Ok , So the first thing we have to do is to create a google firebase account which is absolutely free of cost click here to create account on google firebase and you can refer the following video and screenshot that how to create a firebase account .
Step 2 - Get the server key :
After successfully creating your firebase account and then go to your project setting => cloud messaging tab you will get your server key as shown below .
Step 3 - Create Laravel Project :
Now create a Laravel project using the following command or if you have already have your project then you can skip this step .
composer create-project --prefer-dist laravel/laravel FirebasePushNotification
Step 4 - Setup Route and Controller :
Now setup your routes and controller .
Route::post('/pushnotification','UserController@pushnotification')->name('firebase-pushnotification');
Step 5 - Code to send Notification :
Now you can simple use the following code on your controller or in your php code to send push notification .
public function pushnotification() { try{ $fcmUrl = 'https://fcm.googleapis.com/fcm/send'; $token = "your device token here"; $notification = [ 'title' => 'Your title here', "body" => 'message description', 'sound' => true, ]; $NotificationData = ["message" => $notification,"otherdata" =>'other data here']; $fcmNotification = [ 'to' => $token, 'notification' => $notification, 'data' => $NotificationData ]; // the Authorization token provided below is the server key of the firebase and make sure that the server key token must be same like on your code as well as on the mobile device otherwise you will get an error like mismatchsenderid . $headers = [ 'Authorization: key=AAAAjWQ5TkY:APA91bFiw5slt_uW6R-XS5uuOqT9eSGNwKHlIIZ7RUkJx5ZqofOERGpGO0zIT1Neo4kmtfiQD5oz9a_sl_7cQxVROXCYUl_5xWJCUD697pwpoL3slh3WKHDrbmsE3aZqGJ', 'Content-Type: application/json' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$fcmUrl); 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($fcmNotification)); $notify = curl_exec($ch); curl_close($ch); return $notify; } catch (\Exception $e) { Log::error( 'Failed to send push notification', ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()] ); DB::rollback(); return false; } }
Step 6 - Expected Errors and Solution :
In this example i have show you that how can you send push notification using firebase in Laravel or core php to a mobile device .
Error - ( MismatchSenderId ) :
Sometime you might get an error in an array format with error detail shown as MismatchSenderId in FCM .
Solution :
This error basically arises when your server key is different . Like if the firebase server key used in mobile development team and the firebase server key used in your Laravel or PHP code then you will get this error , so make sure the firebase server key should be same in both the sides .
So that's it now you can hit your route and you will get an push notification on your device .
Thank you for reading this article 😊
For any query do not hesitate to comment 💬