File: /var/www/javago-portal-updates/app/Notifications/OrderReminderNotification.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Http;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification as FirebaseNotification;
use App\Models\Notification as NotificationModel;
class OrderReminderNotification extends Notification
{
use Queueable;
protected $order;
public function __construct($order)
{
$this->order = $order;
}
/**
* Don't use Laravel's built-in notification system
*/
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
$notification = NotificationModel::create([
'sender_id' => $this->order->cafe_id,
'receiver_id' => $notifiable->id,
'reference_id' => $this->order->id,
'notification_type' => 8, // Order reminder
'is_read' => 0,
'created_at' => now()->timestamp,
'updated_at' => now()->timestamp
]);
}
/**
* Directly send Firebase Notification
*/
public function sendFirebaseNotification($notifiable)
{
if (!$notifiable->fcm_token) {
return;
}
$serverKey = env('FCM_SERVER_KEY') ?? 'AAAAR-ppn-E:APA91bEiv6lIoyyV45ZKK2wD8XEGryeafGbUyhuFKhW94Yta71wMl9lZ3tKIjQNCJJWLgYsZfWpEjGD7yLxhYY52pCb-Cp72HP1UDy94doAnAKH8vAtU9l8k7ojJ6EsRkfx5ZjIdsk6A';
$data = [
"to" => $notifiable->fcm_token,
"notification" => [
"title" => "Order Reminder",
"body" => "Your order #{$this->order->id} will arrive in 5 minutes!",
"sound" => "default"
],
"data" => [
"order_id" => $this->order->id
]
];
// Load Firebase Service Account
$firebase = (new Factory)->withServiceAccount(storage_path('app/java-go-380509-firebase-adminsdk-236oo-44f6190a2d.json'));
$messaging = $firebase->createMessaging();
// Create the message
$message = CloudMessage::withTarget('token', $notifiable->fcm_token)
->withNotification(FirebaseNotification::create(
'Order Reminder',
"Your order #{$this->order->order_number} will be ready in 5 minutes!"
))
->withData(['order_id' => (string) $this->order->id]);
// Send the notification
$response = $messaging->send($message);
return json_encode($response);
}
}