Files
aso/app/Notifications/SendNotification.php
Linksehat Staging Server 70fc1579e7 update
2024-07-12 08:41:18 +07:00

96 lines
2.8 KiB
PHP
Executable File

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Laravel\Firebase\Facades\Firebase;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
use NotificationChannels\Fcm\Resources\AndroidNotification;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
class SendNotification extends Notification
{
use Queueable;
private $title;
private $body;
private $data;
public function __construct($title, $body, $data)
{
$this->title = $title;
$this->body = $body;
$this->data = is_array($data) ? $data : (array) $data; // Pastikan data adalah array
}
public function via($notifiable)
{
return [FcmChannel::class];
}
// public function toFcm($notifiable)
// {
// return FcmMessage::create()
// ->setData($this->data)
// ->setNotification([
// 'title' => $this->title,
// 'body' => $this->body,
// ]);
// }
public function toFcm($notifiable)
{
$deviceTokens = $notifiable->routeNotificationFor('fcm');
$notification = [
'title' => $this->title,
'body' => $this->body,
];
// if (count($deviceTokens)){
// foreach($deviceTokens as $token) {
// $message = CloudMessage::withTarget('token', $token)
// ->withNotification($notification) // optional
// ->withData($this->data);
// Firebase::messaging()->send($message);
// }
// }
// $datas = [
// 'channel_id' => 2,
// ]
$dataFcm = FcmMessage::create()
->setToken($deviceTokens[0])
->setData($this->data)
->setNotification(
FcmNotification::create()
->setTitle($this->title)
->setBody($this->body)
)
->setAndroid(
AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
)->setApns(
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
);
return $dataFcm;
}
public function fcmProject($notifiable, $message){
return 'app';
}
}