update send notification fcm

This commit is contained in:
2024-05-24 09:09:21 +07:00
parent 8ae6e44680
commit 8af376cd37
10 changed files with 253 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Models;
namespace App\Models\OLDLMS;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -9,11 +9,14 @@ class NotificationToken extends Model
{
use HasFactory;
protected $connection = 'oldlms';
protected $fillable = [
'origin',
'type',
'token',
'status',
'device_id'
];
protected $hidden = [

View File

@@ -1,8 +1,6 @@
<?php
namespace App\Models\OLDLMS;
use App\Models\NotificationToken;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -16,7 +14,7 @@ use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, SoftDeletes, HasApiTokens, HasRoles, Notifiable, Notifiable;
use HasFactory, SoftDeletes, HasApiTokens, HasRoles, Notifiable;
const CREATED_AT = 'dCreateOn';
const UPDATED_AT = 'dUpdateOn';
@@ -40,6 +38,7 @@ class User extends Authenticatable
'nIDHubunganKeluarga',
'dUpdateOn',
'sIPAddress',
'fcm_token',
];
protected function fullName(): Attribute
@@ -73,4 +72,6 @@ class User extends Authenticatable
{
return $this->morphMany(NotificationToken::class, 'notifiabletoken');
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
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 = $data;
}
public function via($notifiable)
{
return [FcmChannel::class];
}
public function toFcm($notifiable)
{
return FcmMessage::create()
->setData($this->data) // Menggunakan $this->data
->setNotification([
'title' => $this->title,
'body' => $this->body,
]);
}
}