From 4b7337f7cbf034a74fc7c95962d379d8fcdc5815 Mon Sep 17 00:00:00 2001 From: Linksehat Staging Server Date: Mon, 3 Jun 2024 09:09:16 +0700 Subject: [PATCH] bugs fix send notification --- app/Models/NotificationToken.php | 1 + app/Models/User.php | 2 +- app/Notifications/SendNotification.php | 27 +++++++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/Models/NotificationToken.php b/app/Models/NotificationToken.php index 7979b36d..4344aa9e 100644 --- a/app/Models/NotificationToken.php +++ b/app/Models/NotificationToken.php @@ -14,6 +14,7 @@ class NotificationToken extends Model 'type', 'token', 'status', + 'device_id' ]; protected $hidden = [ diff --git a/app/Models/User.php b/app/Models/User.php index f7dde5f6..40a6e3aa 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -114,6 +114,6 @@ class User extends Authenticatable public function routeNotificationForFcm() { - return $this->notificationTokens()->pluck('token')->toArray(); + return $this->notificationTokens()->orderBy('created_at', 'desc')->pluck('token')->toArray(); } } diff --git a/app/Notifications/SendNotification.php b/app/Notifications/SendNotification.php index b2ebdbab..d249bc62 100644 --- a/app/Notifications/SendNotification.php +++ b/app/Notifications/SendNotification.php @@ -54,22 +54,27 @@ class SendNotification extends Notification '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); - } - } + // 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([]) + ->setData($this->data) ->setNotification( FcmNotification::create() - ->setTitle('ini title') - ->setBody('ini body') + ->setTitle($this->title) + ->setBody($this->body) ) ->setAndroid( AndroidConfig::create()