| Server IP : 34.87.99.72 / Your IP : 216.73.217.31 Web Server : Apache/2.4.46 (Unix) OpenSSL/1.1.1n System : Linux zlock-bitnami-lamp-prod-v2-vm 4.19.0-16-cloud-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 User : bitnami ( 1000) PHP Version : 7.4.18 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /opt/bitnami/apache/htdocs/app/app/Http/Services/ |
Upload File : |
<?php
namespace App\Http\Services;
use App\Jobs\PushNotification;
use App\Models\UserNotification;
use App\Models\NotificationMessage;
use App\Models\User;
use App\Models\UserFcmToken;
use App\Http\Services\FcmService;
class NotificationService{
public function __construct(
FcmService $fcmService
)
{
$this->fcmService = $fcmService;
}
######################### User Notification Message ########################################
public function createUserNotification(Array $request, $shouldNotifyUser = false)
{
$notification_message = new NotificationMessage;
$notification_message->type_id = $request['type_id'];
$notification_message->title = $request['title'];
$notification_message->content = $request['content'] ?? null;
$notification_message->on_tap = $request['on_tap'] ?? null;
$notification_message->save();
$users = User::findMany($request['user_id']);
$notification_message->users()->attach($users);
if($shouldNotifyUser){
$this->notifyUser($users, $notification_message, $request['delay_minute'] ?? 0);
}
return $notification_message;
}
public function notifyUser($users, NotificationMessage $notification_message, int $delay_minute = 0)
{
$user_ids = $users->pluck('id');
$target = UserFcmToken::whereHas('user',function($q) use ($user_ids){
$q->whereIn('id', $user_ids);
})->pluck('token')->toArray();
$payload = [
"token" => $target,
"title" => $notification_message->title,
"body" => $notification_message->content,
"isTopic" => false,
"user_ids" => $user_ids,
];
// $this->fcmService->notifyUser($payload); // for debug
PushNotification::dispatch($payload)->delay(now()->addMinutes($delay_minute));
return null;
}
public function listUserNotification(Array $request)
{
$user_id = $request['user_id'];
$notifications = NotificationMessage::whereHas('users',function($q) use ($user_id){
$q->where('id',$user_id);
})
->with([
'userNotifications'=> function($q) use ($user_id){
$q->where('user_id',$user_id);
}
])
->orderBy('created_at',"DESC")
->paginate();
// process data
$notifications->each(function($item){
$item->is_read = $item->userNotifications[0]->is_read;
unset($item->userNotifications);
});
return $notifications;
}
public function readUserNotification(Array $request, $shouldNotifyUser = false)
{
$userNotification = UserNotification::where('user_id',$request['user_id'])
->where('notification_id',$request['notification_id'])
->firstOr( function(){
throw ValidationException::withMessages(
[ "id" => "The selected id is invalid." ]
);
});
$userNotification = UserNotification::where('user_id',$request['user_id'])
->where('notification_id',$request['notification_id'])
->update([
"is_read" => true,
]);
return $userNotification;
}
public function hasUnseenNotification(User $user, $is_bool = true)
{
$latest_notification = UserNotification::where('user_id', $user->id)
->where("is_read", 0)
->count();
return ( $is_bool ? (bool) $latest_notification : (int) $latest_notification );
}
######################### Boardcast Message ########################################
public function boardcastMessageList(Array $request)
{
$notifications = NotificationMessage::where('type_id', NotificationMessage::BOARDCAST)
->orderBy('created_at',"DESC")
->paginate();
return $notifications;
}
public function createBoardcast(Array $request, $sendNow = false)
{
$notification_message = new NotificationMessage;
$notification_message->type_id = $request['type_id'];
$notification_message->title = $request['title'];
$notification_message->content = $request['content'] ?? null;
$notification_message->on_tap = $request['on_tap'] ?? null;
$notification_message->save();
$active_users = User::whereHas('tokens',function($q){
$q->whereNotNull('last_used_at');
})->get('id');
$notification_id = $notification_message->id;
$active_users->chunk(100)->each(function($chuck) use ($notification_id){
$user_notification_chuck = $chuck->map(function($user) use ($notification_id){
return [
"user_id" => $user->id,
"notification_id" => $notification_id,
"created_at" => now(),
"updated_at" => now(),
];
})->toArray();
UserNotification::insert($user_notification_chuck);
});
if($sendNow){
$this->boardcast($notification_message);
}
return $notification_message;
}
public function boardcast(NotificationMessage $notification_message, $target = "all")
{
$payload = [
"to" => $target,
"title" => $notification_message->title,
"body" => $notification_message->content,
"isTopic" => true,
];
// $this->fcmService->sendBoardcast($payload); // for debug
PushNotification::dispatch($payload);
}
}