namespace App\Services;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log; //For error logging
use Exception;
class NotificationService
{
public function sendNotification(array $userIds, string $type, array $data)
{
// Loop through the recipients and create a notification for each
foreach ($userIds as $userId) {
$user = User::find($userId);
if (!$user) {
Log::error("User not found: " . $userId);
continue; // Skip if user not found
}
$notification = Notification::create([
'user_id' => $userId,
'type' => $type,
'data' => $data,
]);
// Dispatch delivery based on user preferences and notification type.
$this->dispatchDelivery($notification, $user);
}
}
private function dispatchDelivery(Notification $notification, User $user)
{
// You can customize delivery preferences per user.
// For example, you might have a "notification_preferences" table.
$this->sendPushNotification($notification, $user); //If user has push token
$this->sendEmailNotification($notification, $user); //If email is enabled
$this->createInAppNotification($notification); // Always create in-app
}
private function sendPushNotification(Notification $notification, User $user)
{
if ($user->expo_push_token) {
try {
$response = $this->sendExpoPush($user->expo_push_token, $notification->data['message'], $notification->data);
$status = 'sent';
$response_data = $response;
} catch (Exception $e) {
Log::error("Push Notification Error: " . $e->getMessage());
$status = 'failed';
$response_data = ['error' => $e->getMessage()];
}
$notification->deliveries()->create([
'channel' => 'push',
'status' => $status,
'response' => $response_data,
]);
}
}
private function sendEmailNotification(Notification $notification, User $user)
{
//Email Logic here, for example
try {
Mail::to($user->email)->send(new \App\Mail\NotificationEmail($notification));
$status = 'sent';
$response_data = ['message_id' => 'example_message_id']; //Replace with actual message id from mail provider
} catch (Exception $e) {
Log::error("Email Notification Error: " . $e->getMessage());
$status = 'failed';
$response_data = ['error' => $e->getMessage()];
}
$notification->deliveries()->create([
'channel' => 'email',
'status' => $status,
'response' => $response_data,
]);
}
private function createInAppNotification(Notification $notification)
{
// In-app notifications are already stored in the database
$notification->deliveries()->create([
'channel' => 'in-app',
'status' => 'created', //Or 'available'
]);
}
private function sendExpoPush(string $expoPushToken, string $message, array $data)
{
// Implementation using Expo Push API
// Requires installing the Expo package: composer require exponent/exponent
// See Expo documentation for API details: https://docs.expo.dev/push-notifications/sending-notifications/
$expo = \ExponentPhp\Expo::normalSetup()->setApiKey(config('services.expo.access_token')); // Ensure you have EXPO_ACCESS_TOKEN in your .env
$notification = ['to' => $expoPushToken, 'sound' => 'default', 'body' => $message, 'data' => $data];
$response = $expo->pushSend($notification);
if ($response->isError()) {
throw new Exception("Expo Push Error: " . $response->getMessage());
}
return $response->getData(); //Return the data from the expo api
}
}