Skip to content

Commit 2e3086f

Browse files
author
Your Name
committed
Angular PWA Course
1 parent 3f74913 commit 2e3086f

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

server/server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ const vapidKeys = {
1313
privateKey: "TODO"
1414
};
1515

16+
17+
/*
18+
19+
TODO - uncomment after generating your VAPID key
20+
1621
webpush.setVapidDetails(
1722
'mailto:example@yourdomain.org',
1823
vapidKeys.publicKey,
1924
vapidKeys.privateKey
2025
);
2126
27+
*/
28+
2229

2330
const app: Application = express();
2431

src/app/lessons/lessons.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
</div>
1111

12-
<h2>All Lessons - V8</h2>
12+
<h2>All Lessons - V10</h2>
1313

1414
<table class="table lessons-list card card-strong">
1515
<tbody>

src/app/lessons/lessons.component.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ export class LessonsComponent implements OnInit {
1515
lessons$: Observable<Lesson[]>;
1616
isLoggedIn$: Observable<boolean>;
1717

18-
readonly VAPID_PUBLIC_KEY = "TODO";
18+
readonly VAPID_PUBLIC_KEY = "BEcGS3q-oXT26Gu20Rk5xQ-b5kQr2mdNeXo0l3YJfxq4r-lki16Cp9CSKF0IWy_hETKgJGJQpH5HfuSE8NkHIlU";
1919

2020
constructor(
2121
private lessonsService: LessonsService,
22+
private swPush: SwPush,
2223
private newsletterService: NewsletterService) {
2324

2425
}
@@ -34,7 +35,20 @@ export class LessonsComponent implements OnInit {
3435

3536
subscribeToNotifications() {
3637

38+
this.swPush.requestSubscription({
39+
serverPublicKey: this.VAPID_PUBLIC_KEY
40+
})
41+
.then(sub => {
3742

43+
console.log("Notification Subscription: ", sub);
44+
45+
this.newsletterService.addPushSubscriber(sub).subscribe(
46+
() => console.log('Sent push subscription object to server.'),
47+
err => console.log('Could not send subscription object to server, reason: ', err)
48+
);
49+
50+
})
51+
.catch(err => console.error("Could not subscribe to notifications", err));
3852

3953
}
4054

src/app/services/newsletter.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class NewsletterService {
1313
}
1414

1515
addPushSubscriber(sub:any) {
16-
16+
return this.http.post('/api/notifications', sub);
1717
}
1818

1919
send() {

0 commit comments

Comments
 (0)