http = $http; $this->apiKey = $apiKey; } /** * Send the given notification. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return void */ public function send($notifiable, Notification $notification) { $message = $notification->toFcm($notifiable); $message->to($notifiable->routeNotificationFor('fcm', $notification)); if (! $this->apiKey || (! $message->topic && ! $message->to)) { return; } $this->http->post(self::API_URI, [ 'headers' => [ 'Authorization' => "key={$this->apiKey}", 'Content-Type' => 'application/json', ], 'json' => $this->buildJsonPayload($message), ]); } protected function buildJsonPayload(FcmMessage $message) { $payload = array_filter([ 'priority' => $message->priority, 'data' => $message->data, 'notification' => $message->notification, 'condition' => $message->condition, ]); if ($message->topic) { $payload['to'] = "/topics/{$message->topic}"; } else { if (is_array($message->to)) { $payload['registration_ids'] = $message->to; } else { $payload['to'] = $message->to; } } return $payload; } }