Added native os notifications
This commit is contained in:
+3
-2
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Http\Controllers\UserFlightController;
|
||||
use App\Settings\SettingsRegistry;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
@@ -12,9 +11,11 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use App\Traits\HasAchievements;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use NotificationChannels\WebPush\HasPushSubscriptions;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
#[Fillable(['name', 'email', 'password', 'distance_unit', 'settings'])]
|
||||
@@ -23,7 +24,7 @@ class User extends Authenticatable
|
||||
{
|
||||
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, HasAchievements, HasApiTokens, HasRoles;
|
||||
use HasFactory, HasAchievements, HasApiTokens, HasRoles, HasPushSubscriptions, Notifiable;
|
||||
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\Notification as NotificationModel;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use NotificationChannels\WebPush\WebPushChannel;
|
||||
use NotificationChannels\WebPush\WebPushMessage;
|
||||
|
||||
class PushNotification extends Notification
|
||||
{
|
||||
public function __construct(
|
||||
protected NotificationModel $notification
|
||||
) {}
|
||||
|
||||
public function via($notifiable): array
|
||||
{
|
||||
return [WebPushChannel::class];
|
||||
}
|
||||
|
||||
public function toWebPush($notifiable, $notification): WebPushMessage
|
||||
{
|
||||
return (new WebPushMessage)
|
||||
->title($this->notification->title)
|
||||
->body($this->notification->body)
|
||||
->icon( '/img/app_icons/icon-192.png')
|
||||
->data([
|
||||
'url' => $this->notification->url,
|
||||
'notification_id' => $this->notification->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,21 @@ namespace App\Observers;
|
||||
|
||||
use App\Events\NotificationCreated;
|
||||
use App\Models\Notification;
|
||||
use App\Notifications\PushNotification;
|
||||
|
||||
class NotificationObserver
|
||||
{
|
||||
public function created(Notification $notification): void
|
||||
{
|
||||
\Log::info('Notification created: ' . $notification->id);
|
||||
|
||||
broadcast(new NotificationCreated($notification));
|
||||
|
||||
try {
|
||||
$notification->user->notify(new \App\Notifications\PushNotification($notification));
|
||||
\Log::info('WebPush notify() called for user ' . $notification->user_id);
|
||||
} catch (\Throwable $e) {
|
||||
\Log::error('WebPush send failed: ' . $e->getMessage(), ['exception' => $e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user