PWA improvements
This commit is contained in:
@@ -49,7 +49,7 @@
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
const subscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: 'BEl62iUYgUivxIkv69yViEuiBIa40HI6F2B5L4h7Q8Y'
|
||||
applicationServerKey: 'BJmNVx0C3XeVxeKGTP9c-Z4HcuZNmdk6QdiLocZgCmb-miCS0ESFO3W2TvJlRhhNAShV63pWA5p36BTVSetyTds'
|
||||
});
|
||||
|
||||
// Send subscription to server
|
||||
|
||||
@@ -843,8 +843,8 @@ function showWebNotification(title, message, icon = '🏆') {
|
||||
if ('Notification' in window && Notification.permission === 'granted') {
|
||||
const notification = new Notification(title, {
|
||||
body: message,
|
||||
icon: '/pictures/favicon.ico',
|
||||
badge: '/pictures/favicon.ico',
|
||||
icon: '/pictures/icon-192.png',
|
||||
badge: '/pictures/icon-192.png',
|
||||
tag: 'ninjacross-achievement',
|
||||
requireInteraction: true
|
||||
});
|
||||
|
||||
@@ -9,15 +9,15 @@
|
||||
"orientation": "portrait",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/pictures/favicon.ico",
|
||||
"src": "/pictures/icon-192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/x-icon",
|
||||
"type": "image/png",
|
||||
"purpose": "any maskable"
|
||||
},
|
||||
{
|
||||
"src": "/pictures/favicon.ico",
|
||||
"src": "/pictures/icon-512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/x-icon",
|
||||
"type": "image/png",
|
||||
"purpose": "any maskable"
|
||||
}
|
||||
],
|
||||
@@ -34,7 +34,7 @@
|
||||
"url": "/index.html",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/pictures/favicon.ico",
|
||||
"src": "/pictures/icon-192.png",
|
||||
"sizes": "192x192"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -36,8 +36,8 @@ self.addEventListener('push', function(event) {
|
||||
|
||||
const options = {
|
||||
body: 'Du hast eine neue Notification!',
|
||||
icon: '/pictures/favicon.ico',
|
||||
badge: '/pictures/favicon.ico',
|
||||
icon: '/pictures/icon-192.png',
|
||||
badge: '/pictures/icon-192.png',
|
||||
vibrate: [100, 50, 100],
|
||||
data: {
|
||||
dateOfArrival: Date.now(),
|
||||
@@ -47,12 +47,12 @@ self.addEventListener('push', function(event) {
|
||||
{
|
||||
action: 'explore',
|
||||
title: 'Dashboard öffnen',
|
||||
icon: '/pictures/favicon.ico'
|
||||
icon: '/pictures/icon-192.png'
|
||||
},
|
||||
{
|
||||
action: 'close',
|
||||
title: 'Schließen',
|
||||
icon: '/pictures/favicon.ico'
|
||||
icon: '/pictures/icon-192.png'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
350
public/test-push.html
Normal file
350
public/test-push.html
Normal file
@@ -0,0 +1,350 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Push Notification Test - Ninja Cross</title>
|
||||
<link rel="icon" type="image/x-icon" href="/pictures/favicon.ico">
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
min-height: 100vh;
|
||||
}
|
||||
.container {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
padding: 30px;
|
||||
border-radius: 15px;
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.status {
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
padding: 15px;
|
||||
border-radius: 10px;
|
||||
margin: 20px 0;
|
||||
}
|
||||
.status.success { background: rgba(34, 197, 94, 0.3); }
|
||||
.status.error { background: rgba(239, 68, 68, 0.3); }
|
||||
.status.warning { background: rgba(245, 158, 11, 0.3); }
|
||||
button {
|
||||
background: linear-gradient(135deg, #00d4ff, #0891b2);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 12px 24px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
margin: 10px 5px;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
button:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
transform: none;
|
||||
}
|
||||
input, textarea {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
margin: 10px 0;
|
||||
font-size: 16px;
|
||||
}
|
||||
.log {
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
padding: 15px;
|
||||
border-radius: 10px;
|
||||
margin-top: 20px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>🧪 Push Notification Test</h1>
|
||||
|
||||
<div id="status" class="status">
|
||||
<strong>Status:</strong> <span id="statusText">Lädt...</span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>1. Service Worker Status</h3>
|
||||
<button onclick="checkServiceWorker()">Service Worker prüfen</button>
|
||||
<button onclick="registerServiceWorker()">Service Worker registrieren</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>2. Notification Permission</h3>
|
||||
<button onclick="requestPermission()">Berechtigung anfordern</button>
|
||||
<button onclick="checkPermission()">Berechtigung prüfen</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>3. Push Subscription</h3>
|
||||
<button onclick="subscribeToPush()">Push abonnieren</button>
|
||||
<button onclick="unsubscribeFromPush()">Push abbestellen</button>
|
||||
<button onclick="checkSubscription()">Subscription prüfen</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>4. Test Notifications</h3>
|
||||
<input type="text" id="testMessage" placeholder="Test-Nachricht" value="Das ist eine Test-Push-Notification!">
|
||||
<button onclick="sendTestPush()">Test-Push senden</button>
|
||||
<button onclick="sendTestWebNotification()">Web-Notification senden</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>5. Push Status</h3>
|
||||
<button onclick="getPushStatus()">Push-Status abrufen</button>
|
||||
</div>
|
||||
|
||||
<div class="log" id="log">
|
||||
<div>Log wird hier angezeigt...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let currentSubscription = null;
|
||||
|
||||
function log(message, type = 'info') {
|
||||
const logDiv = document.getElementById('log');
|
||||
const timestamp = new Date().toLocaleTimeString();
|
||||
const logEntry = document.createElement('div');
|
||||
logEntry.style.color = type === 'error' ? '#ff6b6b' : type === 'success' ? '#51cf66' : '#ffffff';
|
||||
logEntry.textContent = `[${timestamp}] ${message}`;
|
||||
logDiv.appendChild(logEntry);
|
||||
logDiv.scrollTop = logDiv.scrollHeight;
|
||||
}
|
||||
|
||||
function updateStatus(message, type = 'info') {
|
||||
const statusDiv = document.getElementById('status');
|
||||
const statusText = document.getElementById('statusText');
|
||||
statusText.textContent = message;
|
||||
statusDiv.className = `status ${type}`;
|
||||
}
|
||||
|
||||
// Service Worker Functions
|
||||
async function checkServiceWorker() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||
log(`Service Worker registriert: ${registrations.length > 0 ? 'Ja' : 'Nein'}`);
|
||||
if (registrations.length > 0) {
|
||||
const reg = registrations[0];
|
||||
log(`SW Scope: ${reg.scope}`);
|
||||
log(`SW State: ${reg.active ? reg.active.state : 'kein aktiver Worker'}`);
|
||||
}
|
||||
} else {
|
||||
log('Service Worker nicht unterstützt', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function registerServiceWorker() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.register('/sw.js');
|
||||
log('Service Worker erfolgreich registriert', 'success');
|
||||
log(`SW Scope: ${registration.scope}`);
|
||||
} catch (error) {
|
||||
log(`Service Worker Registrierung fehlgeschlagen: ${error.message}`, 'error');
|
||||
}
|
||||
} else {
|
||||
log('Service Worker nicht unterstützt', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Notification Permission Functions
|
||||
async function requestPermission() {
|
||||
if ('Notification' in window) {
|
||||
const permission = await Notification.requestPermission();
|
||||
log(`Notification Permission: ${permission}`, permission === 'granted' ? 'success' : 'warning');
|
||||
updateStatus(`Notification Permission: ${permission}`, permission === 'granted' ? 'success' : 'warning');
|
||||
} else {
|
||||
log('Notifications nicht unterstützt', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function checkPermission() {
|
||||
if ('Notification' in window) {
|
||||
log(`Notification Permission: ${Notification.permission}`);
|
||||
updateStatus(`Notification Permission: ${Notification.permission}`,
|
||||
Notification.permission === 'granted' ? 'success' : 'warning');
|
||||
} else {
|
||||
log('Notifications nicht unterstützt', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Push Subscription Functions
|
||||
async function subscribeToPush() {
|
||||
if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
|
||||
log('Push Manager nicht unterstützt', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
const subscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: 'BJmNVx0C3XeVxeKGTP9c-Z4HcuZNmdk6QdiLocZgCmb-miCS0ESFO3W2TvJlRhhNAShV63pWA5p36BTVSetyTds'
|
||||
});
|
||||
|
||||
currentSubscription = subscription;
|
||||
log('Push Subscription erfolgreich erstellt', 'success');
|
||||
log(`Endpoint: ${subscription.endpoint.substring(0, 50)}...`);
|
||||
|
||||
// Send to server
|
||||
const response = await fetch('/api/v1/public/subscribe', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(subscription)
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
log('Subscription erfolgreich an Server gesendet', 'success');
|
||||
// Store the subscription endpoint for later use
|
||||
localStorage.setItem('pushSubscriptionEndpoint', subscription.endpoint);
|
||||
} else {
|
||||
log(`Server-Fehler: ${result.message}`, 'error');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
log(`Push Subscription fehlgeschlagen: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function unsubscribeFromPush() {
|
||||
if (currentSubscription) {
|
||||
try {
|
||||
await currentSubscription.unsubscribe();
|
||||
currentSubscription = null;
|
||||
log('Push Subscription erfolgreich abbestellt', 'success');
|
||||
} catch (error) {
|
||||
log(`Push Unsubscribe fehlgeschlagen: ${error.message}`, 'error');
|
||||
}
|
||||
} else {
|
||||
log('Keine aktive Subscription gefunden', 'warning');
|
||||
}
|
||||
}
|
||||
|
||||
async function checkSubscription() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
const subscription = await registration.pushManager.getSubscription();
|
||||
|
||||
if (subscription) {
|
||||
currentSubscription = subscription;
|
||||
log('Aktive Push Subscription gefunden', 'success');
|
||||
log(`Endpoint: ${subscription.endpoint.substring(0, 50)}...`);
|
||||
} else {
|
||||
log('Keine Push Subscription gefunden', 'warning');
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Subscription Check fehlgeschlagen: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test Functions
|
||||
async function sendTestPush() {
|
||||
const message = document.getElementById('testMessage').value;
|
||||
|
||||
// First check if we have a subscription
|
||||
if (!currentSubscription) {
|
||||
log('Keine Push Subscription gefunden. Bitte zuerst "Push abonnieren" klicken!', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Use the stored subscription endpoint as identifier
|
||||
const storedEndpoint = localStorage.getItem('pushSubscriptionEndpoint');
|
||||
let userId = 'test-user';
|
||||
if (storedEndpoint) {
|
||||
// Use the endpoint as a unique identifier
|
||||
userId = storedEndpoint.split('/').pop().substring(0, 8);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/v1/public/test-push', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
userId: userId,
|
||||
message: message
|
||||
})
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
log('Test-Push erfolgreich gesendet', 'success');
|
||||
log(`An User ID: ${userId}`, 'success');
|
||||
} else {
|
||||
log(`Test-Push fehlgeschlagen: ${result.message}`, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Test-Push Fehler: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function sendTestWebNotification() {
|
||||
if ('Notification' in window && Notification.permission === 'granted') {
|
||||
const message = document.getElementById('testMessage').value;
|
||||
const notification = new Notification('🧪 Test Web Notification', {
|
||||
body: message,
|
||||
icon: '/pictures/icon-192.png',
|
||||
badge: '/pictures/icon-192.png'
|
||||
});
|
||||
|
||||
notification.onclick = function() {
|
||||
window.focus();
|
||||
notification.close();
|
||||
};
|
||||
|
||||
log('Web-Notification gesendet', 'success');
|
||||
} else {
|
||||
log('Web-Notifications nicht verfügbar oder nicht erlaubt', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function getPushStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/v1/public/push-status');
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
log(`Push Status: ${JSON.stringify(result.data, null, 2)}`, 'success');
|
||||
} else {
|
||||
log(`Push Status Fehler: ${result.message}`, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Push Status Fehler: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize
|
||||
window.addEventListener('load', function() {
|
||||
log('Push Notification Test Seite geladen');
|
||||
checkServiceWorker();
|
||||
checkPermission();
|
||||
checkSubscription();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user