web push notifications
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class WebPushController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'endpoint' => 'required',
|
||||
'keys.p256dh' => 'required',
|
||||
'keys.auth' => 'required',
|
||||
]);
|
||||
|
||||
$endpoint = $request->endpoint;
|
||||
$token = $request->keys['auth'];
|
||||
$key = $request->keys['p256dh'];
|
||||
|
||||
Auth::user()->updatePushSubscription($endpoint, $key, $token);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'endpoint' => 'required',
|
||||
]);
|
||||
|
||||
Auth::user()->deletePushSubscription($request->endpoint);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -13,6 +13,7 @@ use Illuminate\Support\Carbon;
|
||||
use Laravel\Fortify\Contracts\PasskeyUser;
|
||||
use Laravel\Fortify\PasskeyAuthenticatable;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
use NotificationChannels\WebPush\HasPushSubscriptions;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
@@ -32,7 +33,7 @@ use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
class User extends Authenticatable implements PasskeyUser
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable;
|
||||
use HasFactory, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable, HasPushSubscriptions;
|
||||
|
||||
public function dynamics()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
use NotificationChannels\WebPush\WebPushChannel;
|
||||
use NotificationChannels\WebPush\WebPushMessage;
|
||||
|
||||
class NewActivityNotification extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public $activity;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*/
|
||||
public function __construct($activity)
|
||||
{
|
||||
$this->activity = $activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return [WebPushChannel::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the web push representation of the notification.
|
||||
*/
|
||||
public function toWebPush(object $notifiable): WebPushMessage
|
||||
{
|
||||
return (new WebPushMessage)
|
||||
->title('New Activity')
|
||||
->icon('/apple-touch-icon.png')
|
||||
->body($this->activity['content'])
|
||||
->action('View', 'view')
|
||||
->data(['url' => $this->activity['url']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ use App\Models\ReadCursor;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use App\Notifications\NewActivityNotification;
|
||||
|
||||
class ActivityService
|
||||
{
|
||||
/**
|
||||
@@ -54,6 +56,8 @@ class ActivityService
|
||||
'subject_type' => $subject ? get_class($subject) : null,
|
||||
]);
|
||||
|
||||
$this->notify($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
@@ -70,6 +74,21 @@ class ActivityService
|
||||
return $mutation;
|
||||
}
|
||||
|
||||
public function notify(Message $message)
|
||||
{
|
||||
$dynamic = $message->chat->chatable;
|
||||
|
||||
if ($dynamic instanceof Dynamic) {
|
||||
$participants = $dynamic->participants;
|
||||
|
||||
foreach ($participants as $participant) {
|
||||
if ($message->user_id !== $participant->id) {
|
||||
$participant->notify(new NewActivityNotification($message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all activities for a given entity.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user