Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/www/app/Modules/Users/Http/Controllers/Api/ |
<?php
namespace App\Modules\Users\Http\Controllers\Api;
use App\User;
use App\Http\Controllers\Controller;
use App\Modules\Notifications\Models\Notification;
use Illuminate\Http\Request;
use UrbanAirship\Airship;
use UrbanAirship\AirshipException;
use UrbanAirship\UALog;
use UrbanAirship\Push as P;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Illuminate\Support\Facades\Auth;
class ApiNotifications extends Controller
{
/**
* Test notification system
*/
public function send_test(User $user)
{
$sendTo = [];
$devices = $user->devices->where('os','iOS');
foreach($devices as $device)
$sendTo[]=$device->push_token;
$airship = new Airship("nTQIdXYvTmaZE-FhBWbn8A", "Kj7uEeWcTB2K5nA_MkuVng");
try {
$response = $airship->push()
->setAudience(P\all)
->setNotification(P\notification("Hello from php"))
->setDeviceTypes(P\all)
->send();
$response = $airship->push()
->setAudience("6aac8ad0-2d9e-4037-94c0-f5e48351f305")
->setNotification(P\notification("Hello from PHP"))
->setDeviceTypes(P\all)
->send();
dd($response);
} catch (AirshipException $e) {
print_r($e);
}
}
/**
* notification lists
*/
public function notifications(Request $request)
{
$userId = Auth::id();
$page = $request->get('page', 1); // current page number (default: 1)
$limit = $request->get('limit', 10); // number of notifications per request (default: 10)
$offset = ($page - 1) * $limit;
$notifications = Notification::where('user_id', $userId)
->latest()
->skip($offset)
->take($limit)
->get();
foreach ($notifications as $notification) {
if (!empty($notification->data) && is_string($notification->data)) {
$decoded = json_decode($notification->data, true);
// Only replace if JSON decoding was successful
if (json_last_error() === JSON_ERROR_NONE) {
$notification->data = $decoded;
}
}
}
$total = Notification::where('user_id', $userId)->count();
$unreadCount = Notification::where('user_id', $userId)
->where('is_read', 0)
->count();
return response()->json([
'status' => 'success',
'data' => $notifications,
'current_page' => (int) $page,
'per_page' => (int) $limit,
'total' => $total,
'unread_count' => $unreadCount,
'has_more' => $total > ($offset + $notifications->count()),
]);
}
/**
* mark notification as read
*/
public function markNotiAsRead(Request $request)
{
$userId = Auth::id();
$notificationId = $request->input('id'); // optional ID
if ($notificationId) {
// Mark a single notification as read
$notification = Notification::where('user_id', $userId)
->where('id', $notificationId)
->first();
if (!$notification) {
return response()->json(['status' => 'error', 'message' => 'Notification not found'], 404);
}
$notification->is_read = 1;
$notification->save();
return response()->json(['status' => 'success', 'message' => 'Notification marked as read']);
} else {
// Mark all as read
Notification::where('user_id', $userId)
->where('is_read', 0)
->update(['is_read' => 1]);
return response()->json(['status' => 'success', 'message' => 'All notifications marked as read']);
}
}
}