DEV Community

Rabeea Ali
Rabeea Ali

Posted on • Edited on

Send push notifications from Laravel to IOS & Android

In this article, we will see how to send push notifications from a Laravel app to iOS and 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 the app settings icon, then choose project settings

notification Laravel to mobile apps

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

notification Laravel to mobile apps

Step 2: Create the Config file

Now, in your Laravel project, go tothe Config directory & create a new file called fcm.php containing:

<?php

// config/fcm.php

return [
    'token' => "AAAA6ll7Hs4:XXXXXXXXXXXXXXX",
];
Enter fullscreen mode Exit fullscreen mode

As you see above, you should put the token that you get from FCM dashboard in fcm_token.

Step 3: Add a column in the 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();
});
Enter fullscreen mode Exit fullscreen mode

What is the benefit of this column?

Each device should have one token, so the ISO & Android developers should install Firebase SDK on the ap,p 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 store it with the 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 containing:

<?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,
            ]
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

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 is 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',
          ]
      );

    }
}
Enter fullscreen mode Exit fullscreen mode

Extra stuff

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

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

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

3- If you want to use another way, check this PKG out
https://laravel-notification-channels.com/fcm/#contents

Top comments (1)

Collapse
 
mariogafeed profile image
MarioGafeed

I want to send a notifications to all users that I have, but without using a queue job, Can U help me??

OSZAR »