MIF_E31222307/app/Http/Controllers/NotificationController.php

213 lines
6.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\UserNotification;
use App\Services\NotificationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class NotificationController extends Controller
{
public function index()
{
$user = Auth::user();
$notifications = $user->notifications()
->orderBy('created_at', 'desc')
->paginate(20);
$unreadCount = $user->unreadNotifications()->count();
return view('user.notifications.index', compact('notifications', 'unreadCount'));
}
public function markAsRead($id)
{
$success = NotificationService::markAsRead($id, Auth::id());
return response()->json([
'success' => $success,
'message' => $success ? 'Notifikasi ditandai sebagai dibaca' : 'Notifikasi tidak ditemukan'
]);
}
public function markAllAsRead()
{
$count = NotificationService::markAllAsRead(Auth::id());
return response()->json([
'success' => true,
'message' => "{$count} notifikasi ditandai sebagai dibaca"
]);
}
public function delete($id)
{
$notification = UserNotification::where('id', $id)
->where('user_id', Auth::id())
->first();
if ($notification) {
$notification->delete();
return response()->json([
'success' => true,
'message' => 'Notifikasi berhasil dihapus'
]);
}
return response()->json([
'success' => false,
'message' => 'Notifikasi tidak ditemukan'
]);
}
public function getUnreadCount()
{
$count = Auth::user()->unreadNotifications()->count();
return response()->json([
'count' => $count
]);
}
public function getLatestNotifications()
{
$notifications = Auth::user()->notifications()
->orderBy('created_at', 'desc')
->limit(5)
->get();
return response()->json([
'notifications' => $notifications
]);
}
public function testNotification()
{
$user = Auth::user();
$notification = NotificationService::sendGeneralNotification(
$user->id,
'Test Notifikasi',
'Ini adalah test notifikasi dari sistem. Sistem notifikasi berfungsi dengan baik!',
'info',
['test' => true]
);
return response()->json([
'success' => true,
'message' => 'Test notifikasi berhasil dikirim',
'notification' => $notification
]);
}
public function testBrowserNotification()
{
$user = Auth::user();
// Kirim notifikasi ke database
$notification = NotificationService::sendGeneralNotification(
$user->id,
'Test Browser Notification',
'Ini adalah test notifikasi browser yang akan muncul sebagai popup di device Anda.',
'info',
['browser_test' => true, 'timestamp' => now()]
);
// Data untuk browser notification
$notificationData = [
'title' => 'HeartChoice - Test Browser Notification',
'body' => 'Ini adalah test notifikasi popup di device Anda. Jika Anda melihat ini, berarti notifikasi browser berfungsi dengan baik!',
'icon' => asset('logo/baru/dutdut.png'),
'badge' => asset('logo/baru/dutdut.png'),
'tag' => 'test-browser-notification',
'requireInteraction' => false,
'silent' => false,
'vibrate' => [200, 100, 200],
'data' => [
'url' => route('user.notifications'),
'notification_id' => $notification->id,
'type' => 'browser_test'
]
];
return response()->json([
'success' => true,
'message' => 'Test browser notification berhasil dikirim',
'notification' => $notification,
'browser_data' => $notificationData
]);
}
public function sendBackgroundNotification(Request $request)
{
$user = Auth::user();
$title = $request->input('title', 'HeartChoice Notification');
$body = $request->input('body', 'Anda memiliki notifikasi baru!');
$type = $request->input('type', 'info');
// Kirim notifikasi ke database
$notification = NotificationService::sendGeneralNotification(
$user->id,
$title,
$body,
$type,
['background' => true, 'timestamp' => now()]
);
// Data untuk background notification
$notificationData = [
'title' => $title,
'body' => $body,
'icon' => asset('logo/baru/dutdut.png'),
'badge' => asset('logo/baru/dutdut.png'),
'tag' => 'background-notification',
'requireInteraction' => false,
'silent' => false,
'vibrate' => [200, 100, 200],
'data' => [
'url' => route('user.notifications'),
'notification_id' => $notification->id,
'type' => 'background'
]
];
return response()->json([
'success' => true,
'message' => 'Background notification berhasil dikirim',
'notification' => $notification,
'browser_data' => $notificationData
]);
}
public function getAvailableSounds()
{
$audioPath = public_path('audio');
$availableSounds = [];
if (is_dir($audioPath)) {
$files = scandir($audioPath);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
// Hanya ambil file audio
if (in_array($extension, ['mp3', 'wav', 'ogg', 'm4a'])) {
$name = pathinfo($file, PATHINFO_FILENAME);
$availableSounds[] = [
'name' => $name,
'file' => $file,
'path' => asset("audio/{$file}"),
'display_name' => ucfirst(str_replace(['_', '-'], ' ', $name))
];
}
}
}
}
return response()->json([
'sounds' => $availableSounds
]);
}
}