You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
im using it to send notifications to all my devices
im not linking it to any user bc i want to send notification to all devices
i have a table and model named
DeviceToken.php that stores all the devicetokens/fcm_tokens of devices
DeviceToken.php Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class DeviceToken extends Model
{
use Notifiable;
protected $fillable = ['fcm_token'];
public static function getAllTokens(): array
{
return self::pluck('fcm_token')->toArray();
}
}
now i have notification table and i want that whenever a new notification is created then that notification is sent to all devices not to any specific users/devices that have my app installed
so i did this
SendNotification.php Notification Class
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
class SendNotification extends Notification
{
public $title;
public $body;
/**
* Create a new notification instance.
*/
public function __construct($title, $body)
{
$this->title = $title;
$this->body = $body;
}
public function via($notifiable)
{
return [FcmChannel::class];
}
/**
* Get the mail representation of the notification.
*/
public function toFcm($notifiable): FcmMessage
{
return (new FcmMessage(notification: new FcmNotification(
title: $this->title,
body: $this->body
)))
->data(['data1' => 'value', 'data2' => 'value2']) // Additional data
->custom([
'android' => [
'notification' => [
'color' => '#0A0A0A',
],
],
'apns' => [
'fcm_options' => [
'analytics_label' => 'analytics',
],
],
]);
}
}
and thats my observer below which is called whenever a new notification record is created in db
NotificationObserver.php
<?php
namespace App\Observers;
use App\Models\DeviceToken;
use App\Models\Notification;
use App\Notifications\SendNotification;
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification as NotificationFacade;
use NotificationChannels\Fcm\FcmChannel;
class NotificationObserver implements ShouldHandleEventsAfterCommit
{
/**
* Handle the Notification "created" event.
*/
public function created(Notification $notification): void
{
// Fetch all FCM tokens from the device_tokens table
$deviceTokens = DeviceToken::getAllTokens();
if (!empty($deviceTokens)) {
// Send the notification via FCM
try {
// Optionally, chunk the tokens if you have many (e.g., 1000)
$chunkSize = 500;
$chunks = array_chunk($deviceTokens, $chunkSize);
foreach ($chunks as $chunk) {
NotificationFacade::route(FcmChannel::class, $chunk)
->notify(new SendNotification($notification->title, $notification->body));
}
Log::channel("notification")->info("Notification sent to " . count($deviceTokens) . " devices.");
} catch (\Exception $e) {
Log::channel("notification")->error("Error sending notification: " . $e->getMessage());
}
} else {
Log::channel("notification")->warning("No device tokens found");
}
}
}
in logs i see this
[2025-01-30 15:41:25] local.INFO: Notification sent to 3 devices.
[2025-01-30 15:42:14] local.INFO: Notification sent to 3 devices.
no error seems here but not a single device recieve notification
im using it to send notifications to all my devices
im not linking it to any user bc i want to send notification to all devices
i have a table and model named
DeviceToken.php that stores all the devicetokens/fcm_tokens of devices
DeviceToken.php Model
now i have notification table and i want that whenever a new notification is created then that notification is sent to all devices not to any specific users/devices that have my app installed
so i did this
SendNotification.php Notification Class
and thats my observer below which is called whenever a new notification record is created in db
NotificationObserver.php
in logs i see this
no error seems here but not a single device recieve notification
also i tried doing this
and it returns null and nothing more
please help me im stuck with it
The text was updated successfully, but these errors were encountered: