Skip to content

Commit 2acf288

Browse files
committed
Web Push Added.
1 parent c20578d commit 2acf288

File tree

4 files changed

+334
-77
lines changed

4 files changed

+334
-77
lines changed

src/schematics-pwa/cap-utils/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ import { DefaultTreeDocument, DefaultTreeElement, parse as parseHtml } from 'par
44
import { getWorkspace } from '@schematics/angular/utility/config';
55
import { getFileContent } from '@schematics/angular/utility/test';
66
import { NodeDependency } from '@schematics/angular/utility/dependencies';
7+
import * as ts from 'typescript';
78

89

910

1011

12+
13+
export function createOrOverwriteFile(tree: Tree, filePath: string, fileContent: string): void {
14+
if (!tree.exists(filePath)) {
15+
tree.create(filePath, '');
16+
}
17+
tree.overwrite(filePath, fileContent);
18+
}
19+
20+
1121
/** Appends fragment the specified file. */
1222
export function appendToStartFile(host: Tree, filePath: string, styleRule: string) {
1323
const fileBuffer = host.read(filePath);
@@ -26,6 +36,18 @@ export function appendToStartFile(host: Tree, filePath: string, styleRule: strin
2636
host.commitUpdate(recordedChange);
2737
}
2838

39+
40+
/** Appends the given element HTML fragment to the specified HTML file. */
41+
export function appendHtmlElementToTag(host: Tree, htmlFilePath: string, elementHtml: string, side: string = 'right') {
42+
const htmlFileBuffer = host.read(htmlFilePath);
43+
if (!htmlFileBuffer) {
44+
throw new SchematicsException(`Could not read file for path: ${htmlFilePath}`);
45+
}
46+
const htmlContent = htmlFileBuffer.toString();
47+
host.overwrite(`${htmlFilePath}`, (side === 'right') ? htmlContent + elementHtml : elementHtml + htmlContent);
48+
}
49+
50+
2951
/** Appends the given element HTML fragment to the `<body>` element of the specified HTML file. */
3052
export function appendHtmlElementToBody(host: Tree, htmlFilePath: string, elementHtml: string, side: string = 'right') {
3153
const htmlFileBuffer = host.read(htmlFilePath);
@@ -68,6 +90,8 @@ export function appendHtmlElementToBody(host: Tree, htmlFilePath: string, elemen
6890
}
6991
}
7092

93+
94+
7195
/** Adds a class to the body of the document. */
7296
export function addBodyClass(host: Tree, htmlFilePath: string, className: string): void {
7397
const htmlFileBuffer = host.read(htmlFilePath);
@@ -165,3 +189,11 @@ export function getSourceRoot(tree: Tree, options: any): string {
165189
const workspace = getWorkspace(tree);
166190
return `/${workspace.projects[options.clientProject].sourceRoot}`;
167191
}
192+
193+
export function readIntoSourceFile(host: Tree, modulePath: string) {
194+
const text = host.read(modulePath);
195+
if (text === null) {
196+
throw new SchematicsException(`File ${modulePath} does not exist.`);
197+
}
198+
return ts.createSourceFile(modulePath, text.toString('utf-8'), ts.ScriptTarget.Latest, true);
199+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Component } from '@angular/core';
2+
import { SwUpdate } from "@angular/service-worker";
3+
import { PushService } from "./shared/services/push.service";
4+
import { SwPush, PushSubscription } from "@angular/service-worker";
5+
6+
7+
@Component({
8+
selector: 'app-root',
9+
templateUrl: './app.component.html',
10+
styleUrls: ['./app.component.scss']
11+
})
12+
export class AppComponent {
13+
14+
sub: PushSubscription;
15+
16+
readonly VAPID_PUBLIC_KEY = "BLnVk1MBGFBW4UxL44fuoM2xxQ4o9CuxocVzKn9UVmnXZEyPCTEFjI4sALMB8qN5ee67yZ6MeQWjd5iyS8lINAg";
17+
18+
constructor(
19+
private swUpdate: SwUpdate,
20+
private swPush: SwPush,
21+
private pushService: PushService) {
22+
}
23+
24+
ngOnInit() {
25+
if (this.swUpdate.isEnabled) {
26+
this.swUpdate.available.subscribe(() => {
27+
if (confirm("New version available. Load New Version?")) {
28+
window.location.reload();
29+
}
30+
});
31+
}
32+
}
33+
34+
subscribeToNotifications() {
35+
this.swPush.requestSubscription({
36+
serverPublicKey: this.VAPID_PUBLIC_KEY
37+
})
38+
.then(sub => {
39+
this.sub = sub;
40+
console.log("Notification Subscription: ", sub);
41+
this.pushService.addPushSubscriber(sub)
42+
.subscribe(
43+
() => console.log('Sent push subscription object to server.'),
44+
err => console.log('Could not send subscription object to server, reason: ', err)
45+
);
46+
})
47+
.catch(err => console.error("Could not subscribe to notifications", err));
48+
}
49+
50+
sendNewsletter() {
51+
console.log("Sending Newsletter to all Subscribers ...");
52+
this.pushService.send().subscribe();
53+
}
54+
55+
56+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
import { Injectable } from "@angular/core";
4+
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
5+
6+
7+
@Injectable({
8+
"providedIn": "root"
9+
})
10+
export class PushService {
11+
private actionUrl: string;
12+
private httpOptions: any;
13+
14+
constructor(private http: HttpClient) {
15+
this.httpOptions = {
16+
headers: new HttpHeaders({
17+
'Content-Type': 'application/json'
18+
}),
19+
observe: "response"
20+
};
21+
22+
this.actionUrl = `http://localhost:4000`;
23+
}
24+
25+
addPushSubscriber(sub:any) {
26+
return this.http.post(`${this.actionUrl}/api/notifications`, sub);
27+
}
28+
29+
send() {
30+
return this.http.post(`${this.actionUrl}/api/newsletter`, null);
31+
}
32+
33+
}
34+
35+

0 commit comments

Comments
 (0)