id) ->where('app_type', $notification->target()->value) ->where('active', true) ->pluck('token') ->toArray(); if (empty($tokens)) { return; } $message = CloudMessage::new()->withNotification( Notification::create($notification->title(), $notification->body()) ); $report = $this->messaging->sendMulticast($message, $tokens); $this->deactivateInvalidTokens($report->invalidTokens()); if ($report->successes()->count() > 0) { $this->log($user, $notification); } } /** * Envia para uma coleção de usuários, respeitando cooldowns. * Usuários sem tokens ativos são ignorados silenciosamente. */ public function sendToUsers(Collection $users, BasePushNotification $notification): void { foreach ($users as $user) { $this->sendToUser($user, $notification); } } private function log(User $user, BasePushNotification $notification): void { PushNotificationLog::create([ 'label' => $notification->label(), 'user_id' => $user->id, 'target' => $notification->target()->value, 'category' => $notification->category()->value, 'sent_at' => now(), ]); } private function deactivateInvalidTokens(array $tokens): void { if (empty($tokens)) { return; } DeviceToken::whereIn('token', $tokens)->update(['active' => false]); } }