Send push notifications from Laravel to IOS & Android

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: …


This content originally appeared on DEV Community and was authored by Rabeea Ali

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 the choose project settings

Image description

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

Image description

Step 2: Create Config file

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

<?php

// config/fcm.php

return [
    'fcm_token' => "AAAA6ll7Hs4:XXXXXXXXXXXXXXX",
];

As you see here you should putt the token that you get from DCM dashboard here.

Step 3: Add a column in users table

Now got 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 should install SDK of Firebase on the app and each time a new user joins your app he 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 is 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 called Services and inside this folder create a file called FCM Service.php contains:

<?php

namespace App\Services;

class FCMService
{ 
    public static function send($token, $notification, $data)
    {
        $fields = [
            "to" => $token,
            "priority" => 10,
            'notification' => $notification,
            'data' => $data,
            'vibrate' => 1,
            'sound' => 1
        ];

        $headers = [
            'accept: application/json',
            'Content-Type: application/json',
            'Authorization: key=' . config('fcm.token')
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        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($fields));
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}

Step 5: Send notifications to apps

Now in your controller, you can use FCMService class to send notifications to apps, in the 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',
          ],
          [
            'message' => 'Extra Notification Data'
          ],
      );

    }
}

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.

That it 🥳🚀


This content originally appeared on DEV Community and was authored by Rabeea Ali


Print Share Comment Cite Upload Translate Updates
APA

Rabeea Ali | Sciencx (2022-01-18T21:28:05+00:00) Send push notifications from Laravel to IOS & Android. Retrieved from https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/

MLA
" » Send push notifications from Laravel to IOS & Android." Rabeea Ali | Sciencx - Tuesday January 18, 2022, https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/
HARVARD
Rabeea Ali | Sciencx Tuesday January 18, 2022 » Send push notifications from Laravel to IOS & Android., viewed ,<https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/>
VANCOUVER
Rabeea Ali | Sciencx - » Send push notifications from Laravel to IOS & Android. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/
CHICAGO
" » Send push notifications from Laravel to IOS & Android." Rabeea Ali | Sciencx - Accessed . https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/
IEEE
" » Send push notifications from Laravel to IOS & Android." Rabeea Ali | Sciencx [Online]. Available: https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/. [Accessed: ]
rf:citation
» Send push notifications from Laravel to IOS & Android | Rabeea Ali | Sciencx | https://www.scien.cx/2022/01/18/send-push-notifications-from-laravel-to-ios-android/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.