Send push notifications from Laravel to IOS & Android
Tags: laravel, notifications, fcm, firebase
Published: 2022-01-18 21:28:05

---
title: Send push notifications from Laravel to IOS & Android
published: true
description: how to send notifications from Laravel to apps using FCM 
tags: Laravel, notifications, FCM, Firebase
// cover_image: https://direct_url_to_image.jpg
---

## NOTE: Caution: Sending messages (including upstream messages) with the FCM XMPP and HTTP legacy APIs was deprecated on June 20, 2023, and will be removed in June 2024. 

https://firebase.google.com/docs/cloud-messaging/http-server-ref

================================

In this article, we will see how send push notifications from Laravel app to IOS & Android apps using FCM (Firebase Cloud Messaging).

###Prerequisites: 
• A Laravel project
• Firebase account
• Create a new app in Firebase

## Step 1: Get `fcm_token` from Firebase

Click on app settings icon then choose `project settings`

![notification Laravel to mobile apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x63wlrvomgu59rwpot7j.png)

Then from tabs go to `Cloud Messaging` and you will get the token there. Copy it.

![notification Laravel to mobile apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m16ob04d9qul8wtgiwyf.png)

## Step 2: Create Config file

Now in your Laravel project go to `Config` directory & create a new file called `fcm.php` contains:

```
<?php

// config/fcm.php

return [
    'token' => "AAAA6ll7Hs4:XXXXXXXXXXXXXXX",
];
```
As you see above, you should put the token that you get from FCM dashboard in `fcm_token`.

## Step 3: Add a column in `users` table
Now go to `users` migration & add a new column `fcm_token`:

```
Schema::create('users', function (Blueprint $table) {
    $table->id();
    .....
    $table->string('fcm_token')->nullable();
    $table->timestamps();
});
```
### What is the benefit of this column?

Each device should have one token, so ISO & Android develpoer should install Firebase SDK on the app and each time a new user joins your app, they should generate a token for the user device then send it to your API as a Backend to save this token in the user's table.

> I prefer the way when any user register in your apps the mobile developer send me a token of this user and I just store it with user's information.

## Step 4: Make `services` folder

In the `app` directory create a new folder `Services` and inside it create a file `FCMService.php` contains:

```
<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class FCMService
{ 
    public static function send($token, $notification)
    {
        Http::acceptJson()->withToken(config('fcm.token'))->post(
            'https://fcm.googleapis.com/fcm/send',
            [
                'to' => $token,
                'notification' => $notification,
            ]
        );
    }
}
```

## Step 5: Send notifications to apps
Now in your controller, you can use `FCMService` class to send notifications to apps, in my case I have `UserController` the code going to be as follows:

```
<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Services\FCMService;
use App\Http\Controllers\Controller;

class UserController extends Controller 
{
    public function sendNotificationrToUser($id)
    {
       // get a user to get the fcm_token that already sent.               from mobile apps 
       $user = User::findOrFail($id);
        
      FCMService::send(
          $user->fcm_token,
          [
              'title' => 'your title',
              'body' => 'your body',
          ]
      );

    }
}
```

## Extra stuff 

1 - If you want to send an image with the notification you can easily do it with store the image in a place( your server or something like S3 from AWS) then get the full path of an image and add with the second array of notification as follow:

```
 // store the image & get the URL `$image_url`
 FCMService::send(
    $user->fcm_token,
    [
        'title' => 'your title',
        'body' => 'your body',
        'image' => $image_url
    ],
);
```

2- If you want to send a notifications to all users that you have from your `admin panel` or somewhere, you need use a queue job especially if you have a large amount of users in your database.