feat: 添加推送通知功能,支持Web Push订阅和发送
This commit is contained in:
@@ -21,3 +21,25 @@ self.addEventListener("fetch", (event) => {
|
||||
// If needed in future, add logic here, but keep it disabled for now.
|
||||
return;
|
||||
});
|
||||
|
||||
self.addEventListener('push', function(event) {
|
||||
if (event.data) {
|
||||
const payload = event.data.json();
|
||||
const title = payload.title || 'New Notification';
|
||||
const options = {
|
||||
body: payload.body,
|
||||
icon: payload.icon || '/favicon.png',
|
||||
badge: payload.badge || '/favicon.png',
|
||||
data: payload.url || '/'
|
||||
};
|
||||
|
||||
event.waitUntil(self.registration.showNotification(title, options));
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', function(event) {
|
||||
event.notification.close();
|
||||
event.waitUntil(
|
||||
clients.openWindow(event.notification.data)
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user