Last active
February 16, 2023 19:33
-
-
Save jhades/4f9b93daa3469ba65c60e1e47b1a9f9b to your computer and use it in GitHub Desktop.
Angular Push Notifications - https://blog.angular-university.io/angular-push-notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"publicKey":"BLBx-hf2WrL2qEa0qKb-aCJbcxEvyn62GDTyyP9KTS5K7ZL0K7TfmOKSPqp8vQF0DaG8hpSBknz_x3qf5F4iEFo", | |
"privateKey":"PkVHOUKgY29NM7myQXXoGbp_bH_9j-cxW5cO-fGcSsA" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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==" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 yeah same issues here.
@jhades any updates
I too have that issue. Any updates?
It is user defuned service.
the complete code here
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
Where is NewsletterService defined?