admin管理员组文章数量:1315079
im using laravel-notification-channels/fcm package to send notifications to all my 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
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
please help me im stuck with it
本文标签:
版权声明:本文标题:php - notification not sending to devices using in laravel using fcm by laravel-notification-channelsfcm - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971139a2407844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论