{"id":1355,"date":"2024-09-03T15:51:13","date_gmt":"2024-09-03T15:51:13","guid":{"rendered":"https:\/\/quizonlines.com\/blog-temp\/?p=1355"},"modified":"2026-06-05T09:11:08","modified_gmt":"2026-06-05T09:11:08","slug":"fcm-notification-google-laravel-11","status":"publish","type":"post","link":"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/","title":{"rendered":"Set Up Firebase Cloud Messaging (FCM) in Laravel 11 for Notifications Using Google Services"},"content":{"rendered":"<p>How to implement push notification in laravel with a simple documentation and handle them in mobile side<\/p>\n<p>In this tutorial, we\u2019ll dive into Firebase Cloud Messaging (FCM) and how to integrate it with Laravel 11 to send notifications. We&#8217;ll cover advanced features like scheduling notifications, subscribing users to topics, and sending data-only messages to improve your application&#8217;s engagement.<\/p>\n<p>Whether you need to send real-time updates, group notifications by topic, or send background data silently, this guide has everything you need.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#Step_1_Generate_Firebase_Service_Account_Key\" >Step 1: Generate Firebase Service Account Key<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#Step_2_Place_the_JSON_File_in_Your_Laravel_Project\" >Step 2: Place the JSON File in Your Laravel Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#Step_3_Set_Up_the_Environment_Variable\" >Step 3: Set Up the Environment Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#Full_Code_Integration\" >Full Code Integration<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#Explanation_of_the_Code\" >Explanation of the Code:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#Summary\" >Summary<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/debugspot.com\/blogs\/fcm-notification-google-laravel-11\/#You_may_also_find_interesting\" >You may also find interesting:<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_1_Generate_Firebase_Service_Account_Key\"><\/span>Step 1: Generate Firebase Service Account Key<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To set up Firebase credentials for use in your Laravel project, follow these steps:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Go to the <a href=\"https:\/\/console.firebase.google.com\/u\/0\/?pli=1\" target=\"_blank\" rel=\"noopener\">Firebase Console<\/a>.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2024\/09\/go-to-firebase.png\" alt=\"Laravel 11 FCM notification scheduling example\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Select your project.<\/li>\n<li>In the project settings (gear icon), navigate to &#8220;Service accounts&#8221;.<\/li>\n<li>Click on &#8220;Generate new private key<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2024\/09\/generate-fcm-token.png\" alt=\"null\" \/><\/p>\n<ol>\n<li>A JSON file containing your service account credentials will be downloaded.<\/li>\n<\/ol>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_2_Place_the_JSON_File_in_Your_Laravel_Project\"><\/span>Step 2: Place the JSON File in Your Laravel Project<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Now, store the downloaded JSON file in a secure location. You can create a folder named firebase inside the storage directory and place the JSON file there:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\/storage\/firebase\/service-account.json <\/code><\/pre>\n<p>Storing it in a secure directory like storage ensures that your credentials are safe and not publicly accessible.<\/p>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_3_Set_Up_the_Environment_Variable\"><\/span>Step 3: Set Up the Environment Variable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Next, to make your credentials accessible in Laravel, set an environment variable in the .env file:<\/p>\n<ol>\n<li>Open your .env file.<\/li>\n<li>Add an environment variable to point to the location of the service account JSON file. For example:<\/li>\n<\/ol>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">FIREBASE_CREDENTIALS=storage\/firebase\/service-account.json<\/code><\/pre>\n<p>This step allows you to reference the credentials file in your Laravel code easily.<\/p>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Full_Code_Integration\"><\/span>Full Code Integration<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>With the environment variable set, you can now integrate the code to send FCM notifications. Below is a PHP function that demonstrates how to send notifications via FCM using a Google access token.<\/p>\n<p>FCM allows you to group multiple users by &#8220;topics&#8221; and send notifications to those users as a group.<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\npublic static function FCM($fcm_id, $notification) {\r\n$fcmUrl = 'https:\/\/fcm.googleapis.com\/v1\/projects\/your-project-name\/messages:send';\r\n$token = is_array($fcm_id) ? $fcm_id : array($fcm_id);\r\n$bearer_token = self::getGoogleAccessToken();\r\nforeach ($token as $item) {\r\n    $fcmNotification = [\r\n        'message' =&gt; [\r\n            \"token\" =&gt; $item,\r\n            \"data\" =&gt; $notification,\r\n            'notification' =&gt; [\r\n                \"title\" =&gt; $notification['title'],\r\n                \"body\" =&gt; $notification['message'],\r\n            ],\r\n        ]\r\n    ];\r\n        $headers = [\r\n            'Authorization: Bearer ' . $bearer_token,\r\n            'Content-Type: application\/json'\r\n        ];\r\n\r\n        $ch = curl_init();\r\n        curl_setopt($ch, CURLOPT_URL, $fcmUrl);\r\n        curl_setopt($ch, CURLOPT_POST, true);\r\n        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\r\n        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));\r\n        $result = curl_exec($ch);\r\n        curl_close($ch);\r\n\r\n        $resultData = json_decode($result, true);\r\n        \\Log::info('Fcm notification send fcm_id = ' . $fcm_id . \" ::::\", [$resultData]);\r\n    }\r\n}\r\n\r\npublic static function getGoogleAccessToken() {\r\n    $credentialsFilePath = base_path(env('FIREBASE_CREDENTIALS'));\r\n    $client = new \\Google_Client();\r\n    $client-&gt;setAuthConfig($credentialsFilePath);\r\n    $client-&gt;addScope('https:\/\/www.googleapis.com\/auth\/firebase.messaging');\r\n    $client-&gt;refreshTokenWithAssertion();\r\n    $token = $client-&gt;getAccessToken();\r\n    return $token['access_token'];\r\n}\r\n<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Explanation_of_the_Code\"><\/span>Explanation of the Code:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ol>\n<li><strong>FCM Function:<\/strong> Sends notifications to multiple FCM tokens.<\/li>\n<li><strong>getGoogleAccessToken Function:<\/strong> Retrieves the Google access token required to authenticate the request<\/li>\n<\/ol>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Integrating FCM with Laravel 11 involves generating a Firebase service account key, securely placing it in your project, setting up the environment variable, and implementing the code to send notifications. By following these steps, you can effectively send notifications via Google using Laravel 11.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"You_may_also_find_interesting\"><\/span>You may also find interesting:<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p><a title=\"Custom Middleware in Laravel 11\" href=\"https:\/\/debugspot.com\/blogs\/create-custom-middleware-in-laravel-11\/\" target=\"_blank\" rel=\"noopener\">Custom Middleware in Laravel 11<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to implement push notification in laravel with a simple documentation and handle them in mobile side In this tutorial, we\u2019ll dive into Firebase Cloud Messaging (FCM) and how to integrate it with Laravel 11 to send notifications. We&#8217;ll cover advanced features like scheduling notifications, subscribing users to topics, and sending data-only messages to improve your application&#8217;s engagement. Whether you need to send real-time updates, group notifications by topic, or send background data silently, this guide has everything you need. Step 1: Generate Firebase Service Account Key To set up Firebase credentials for use in your Laravel project, follow these steps: Go to the Firebase Console. Select your project. In the project settings (gear icon), navigate to &#8220;Service accounts&#8221;. Click on &#8220;Generate new private key A JSON file containing your service account credentials will be downloaded. Step 2: Place the JSON File in Your Laravel Project Now, store the downloaded JSON file in a secure location. You can create a folder named firebase inside the storage directory and place the JSON file there: \/storage\/firebase\/service-account.json Storing it in a secure directory like storage ensures that your credentials are safe and not publicly accessible. Step 3: Set Up the Environment Variable Next, &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1936,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_lock_modified_date":false,"footnotes":""},"categories":[3],"tags":[21,25,22,23,24],"class_list":["post-1355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-fcm-laravel","tag-firebase-cloud-messaging","tag-firebase-laravel","tag-google-fcm","tag-laravel-push-notifications"],"acf":[],"_links":{"self":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1355"}],"collection":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/comments?post=1355"}],"version-history":[{"count":22,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1355\/revisions"}],"predecessor-version":[{"id":1357,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1355\/revisions\/1357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media\/1936"}],"wp:attachment":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media?parent=1355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/categories?post=1355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/tags?post=1355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}