From 5013d7a4de67ee5092ff04021bcc1ced8a7518ee Mon Sep 17 00:00:00 2001 From: jinleileiking Date: Fri, 16 Apr 2021 16:56:24 +0800 Subject: [PATCH] add more logs for learning --- app/config/webpush.ts | 4 ++-- app/controllers/subscriptionController.ts | 18 +++++++++++++++--- client/index.js | 2 +- client/sw.js | 7 +++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/config/webpush.ts b/app/config/webpush.ts index 56de4bd..0560231 100644 --- a/app/config/webpush.ts +++ b/app/config/webpush.ts @@ -1,7 +1,7 @@ import webpush from 'web-push'; -const publicVapidKey = 'BMrfFtMtL9IWl9vchDbbbYzJlbQwplyZ_fbv8Pei8gPNna_Dr1O-Ng7U7fy0LLqz5RKIxEytTIzyk6TLrcKbN30'; -const privateVapidKey = 'E5gpbs9Y6r5TscHC64Ce9-hXojA9I1qQL0kuvX8Jz5Y'; +const publicVapidKey = 'BBrX9faJjI6fnzLWb102Kmjn9w7q8xogB864Urp84NbljEz-FWD7lwe3gqwQgWBg2Zn9CIu2Tjm51O31MmalSXw'; +const privateVapidKey = '_CprP8t4DFikcowAKEDDjXY8Nmm1Pbc1I2nygD9-PPs'; export default (): void => { webpush.setVapidDetails( diff --git a/app/controllers/subscriptionController.ts b/app/controllers/subscriptionController.ts index b225672..053f0db 100644 --- a/app/controllers/subscriptionController.ts +++ b/app/controllers/subscriptionController.ts @@ -27,7 +27,7 @@ export const remove = async ( return; } - const successful = await subscriptionRepository.deleteByEndpoint(endpoint); + const successful = await subscriptionRepository.deleteByEndpoint(endpoint); if (successful) { res.sendStatus(200); } else { @@ -49,13 +49,25 @@ export const broadcast = async ( const subscriptions = await subscriptionRepository.getAll(); const notifications: Promise[] = []; + + + + subscriptions.forEach((subscription) => { - notifications.push(webpush.sendNotification(subscription, JSON.stringify(notification))); + console.log(`sub:${subscription}`); + const options = { + proxy: 'http://localhost:7890', + }; + + var ret = webpush.sendNotification(subscription, JSON.stringify(notification), options); + notifications.push(ret); }); - await Promise.all(notifications); + let rets = await Promise.all(notifications); + console.log(rets); res.sendStatus(200); } catch (e) { + console.log(e); next(e); } }; diff --git a/client/index.js b/client/index.js index 015a647..ba2e729 100644 --- a/client/index.js +++ b/client/index.js @@ -5,7 +5,7 @@ if ('serviceWorker' in navigator) { }); } -const publicVapidKey = 'BMrfFtMtL9IWl9vchDbbbYzJlbQwplyZ_fbv8Pei8gPNna_Dr1O-Ng7U7fy0LLqz5RKIxEytTIzyk6TLrcKbN30'; +const publicVapidKey = 'BBrX9faJjI6fnzLWb102Kmjn9w7q8xogB864Urp84NbljEz-FWD7lwe3gqwQgWBg2Zn9CIu2Tjm51O31MmalSXw'; const urlBase64ToUint8Array = (base64String) => { const padding = '='.repeat((4 - base64String.length % 4) % 4); diff --git a/client/sw.js b/client/sw.js index edc9713..c4ae1bf 100644 --- a/client/sw.js +++ b/client/sw.js @@ -1,7 +1,6 @@ self.addEventListener('push', (e) => { const data = e.data.json(); - self.registration.showNotification(data.title, { - body: data.body, - icon: data.icon, - }); + console.log(data); + + self.registration.showNotification(data.title); });