PHP code example of laravelwudel / laravelwudel-notif
1. Go to this page and download the library: Download laravelwudel/laravelwudel-notif library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laravelwudel / laravelwudel-notif example snippets
use LaravelWudel\LaravelWudelNotif\Facades\PushNotification;
// Kirim notifikasi ke user tertentu
PushNotification::sendToUser($user, 'Judul', 'Pesan notifikasi');
// Kirim notifikasi ke semua user
PushNotification::sendToAll('Judul', 'Pesan broadcast');
// Kirim dengan data tambahan
PushNotification::sendToUser($user, 'Judul', 'Pesan', [
'url' => '/dashboard',
'action' => 'view'
]);
use LaravelWudel\LaravelWudelNotif\Services\WebPushService;
class NotificationController extends Controller
{
public function __construct(private WebPushService $pushService) {}
public function sendNotification(Request $request)
{
$user = auth()->user();
$sent = $this->pushService->sendToUser(
$user,
$request->title,
$request->message
);
return response()->json(['sent' => $sent]);
}
}
// User model
public function pushSubscriptions()
{
return $this->hasMany(PushSubscription::class);
}
// PushSubscription model
public function user()
{
return $this->belongsTo(User::class);
}