@use('Namu\WireChat\Facades\WireChat') @php $activeGuard = session('active_guard', 'web'); $authUser = Auth::guard($activeGuard)->user(); @endphp @if($authUser && WireChat::notificationsEnabled())
{ // Service worker is fully ready registration.active.postMessage({ type: 'SHOW_NOTIFICATION', title: title, options: options }); }) .catch(error => { // Fallback to regular notifications this.newNotification(title, options); }); } else { this.newNotification(title, options); } }, newNotification(title,options){ const notification= new Notification(title, options); notification.onclick = (event) => { event.preventDefault(); const convId = message.conversation_id || 'default'; const windowName = 'wirechat-conversation'; const url = event.currentTarget.data.url; const openedWindow = window.open(url, windowName); if (openedWindow) { openedWindow.focus(); } //Close current notification event.currentTarget.close(); }; }, registerServiceWorker() { if ('serviceWorker' in navigator) { navigator.serviceWorker.register(`{{asset(config('wirechat.notifications.main_sw_script','sw.js'))}}`) .then(reg => {}) .catch(err => {}); } } }" x-init=" registerServiceWorker(); userId = @js($authUser->id); encodedType = @js(\Namu\WireChat\Helpers\MorphClassResolver::encode($authUser->getMorphClass())); {{-- We listen to notify participant event --}} Echo.private(`participant.${encodedType}.${userId}`) .listen('.Namu\\WireChat\\Events\\NotifyParticipant', (e) => { {{--Ignore if user is currently open in the chat --}} if (e.redirect_url !== window.location.href) { if (!('Notification' in window)) { } else if (Notification.permission === 'granted') { showNotification(e); } else if (Notification.permission !== 'denied') { Notification.requestPermission().then(permission => { if (permission === 'granted') { showNotification(e); } }); } } }); document.addEventListener('chat-opened', (event) => { const conversation = event.detail.conversation; const tag = 'wirechat-notification-' + conversation; if (navigator.serviceWorker.controller) { navigator.serviceWorker.controller.postMessage({ type: 'CLOSE_NOTIFICATION', tag: tag }); } }); ">
@endif