Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active February 16, 2023 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhades/4f9b93daa3469ba65c60e1e47b1a9f9b to your computer and use it in GitHub Desktop.
Save jhades/4f9b93daa3469ba65c60e1e47b1a9f9b to your computer and use it in GitHub Desktop.
{
"publicKey":"BLBx-hf2WrL2qEa0qKb-aCJbcxEvyn62GDTyyP9KTS5K7ZL0K7TfmOKSPqp8vQF0DaG8hpSBknz_x3qf5F4iEFo",
"privateKey":"PkVHOUKgY29NM7myQXXoGbp_bH_9j-cxW5cO-fGcSsA"
}
@Component({
selector: 'app-root',
template: `
<button class="button button-primary" (click)="subscribeToNotifications()">
Subscribe
</button>
`})
export class AppComponent {
readonly VAPID_PUBLIC_KEY = "BLBx-hf2WrL2qEa0qKb-aCJbcxEvyn62GDTyyP9KTS5K7ZL0K7TfmOKSPqp8vQF0DaG8hpSBknz_x3qf5F4iEFo";
constructor(
private swPush: SwPush,
private newsletterService: NewsletterService) {}
subscribeToNotifications() {
this.swPush.requestSubscription({
serverPublicKey: this.VAPID_PUBLIC_KEY
})
.then(sub => this.newsletterService.addPushSubscriber(sub).subscribe())
.catch(err => console.error("Could not subscribe to notifications", err));
}
}
{
"endpoint": "https://fcm.googleapis.com/fcm/send/cbx2QC6AGbY:APA91bEjTzUxaBU7j-YN7ReiXV-MD-bmk2pGsp9ZVq4Jj0yuBOhFRrUS9pjz5FMnIvUenVqNpALTh5Hng7HRQpcUNQMFblTLTF7aw-yu1dGqhBOJ-U3IBfnw3hz9hq-TJ4K5f9fHLvjY",
"expirationTime": null,
"keys": {
"p256dh": "BOXYnlKnMkzlMc6xlIjD8OmqVh-YqswZdut2M7zoAspl1UkFeQgSLYZ7eKqKcx6xMsGK7aAguQbcG9FMmlDrDIA=",
"auth": "if-YFywyb4g-bFB1hO9WMw=="
}
}
const webpush = require('web-push');
const vapidKeys = {
"publicKey":"BLBx-hf2WrL2qEa0qKb-aCJbcxEvyn62GDTyyP9KTS5K7ZL0K7TfmOKSPqp8vQF0DaG8hpSBknz_x3qf5F4iEFo",
"privateKey":"PkVHOUKgY29NM7myQXXoGbp_bH_9j-cxW5cO-fGcSsA"
};
webpush.setVapidDetails(
'mailto:example@yourdomain.org',
vapidKeys.publicKey,
vapidKeys.privateKey
);
const app: Application = express();
app.route('/api/newsletter').post(sendNewsletter);
export function sendNewsletter(req, res) {
const allSubscriptions = ... get subscriptions from database
console.log('Total subscriptions', allSubscriptions.length);
const notificationPayload = {
"notification": {
"title": "Angular News",
"body": "Newsletter Available!",
"icon": "assets/main-page-logo-small-hat.png",
"vibrate": [100, 50, 100],
"data": {
"dateOfArrival": Date.now(),
"primaryKey": 1
},
"actions": [{
"action": "explore",
"title": "Go to the site"
}]
}
};
Promise.all(allSubscriptions.map(sub => webpush.sendNotification(
sub, JSON.stringify(notificationPayload) )))
.then(() => res.status(200).json({message: 'Newsletter sent successfully.'}))
.catch(err => {
console.error("Error sending notification, reason: ", err);
res.sendStatus(500);
});
}
@cyraid
Copy link

cyraid commented Jul 5, 2018

Where is NewsletterService defined?

@MarkJAmes1217
Copy link

@cyraid yeah same issues here.

@MarkJAmes1217
Copy link

@jhades any updates

@santhosh-saminathan
Copy link

I too have that issue. Any updates?

@tabebqena
Copy link

tabebqena commented Oct 25, 2018

It is user defuned service.
the complete code here

@anil602-coder
Copy link

Unable to register service worker while running this.
Untitled

@AbdElsamie
Copy link

AbdElsamie commented Feb 16, 2023

I too have that issue. Any updates?

It should be something like this:

public newsletterService(sub: any): void { this._http.post('url to server', { sub }); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment