An Interest In:
Web News this Week
- April 12, 2024
- April 11, 2024
- April 10, 2024
- April 9, 2024
- April 8, 2024
- April 7, 2024
- April 6, 2024
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: Get fcm_token
from Firebase
Click on app settings the choose project settings
Then from tabs go to Cloud Messaging
and you will get the token there. Copy it.
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.phpreturn [ '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:
<?phpnamespace 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:
<?phpnamespace 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
Original Link: https://dev.to/rabeeaali/send-push-notifications-from-laravel-to-ios-android-29b4
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To