{"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-03-16T10:34:47","modified_gmt":"2026-03-16T10:34:47","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<h3 class=\"wp-block-heading\">Step 1: Generate Firebase Service Account Key<\/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\">Step 2: Place the JSON File in Your Laravel Project<\/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\">Step 3: Set Up the Environment Variable<\/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\">Full Code Integration<\/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>Explanation of the Code:<\/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\">Summary<\/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>You may also find interesting:<\/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":1914,"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":1911,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1355\/revisions\/1911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media\/1914"}],"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}]}}