Skip to content

Commit

Permalink
added push notifications!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
harshbaldwa committed Sep 12, 2019
1 parent 8fb72d1 commit 3a4cbd4
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
103 changes: 103 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"lodash": "^4.17.15",
"mongoose": "^5.6.9",
"mongoose-unique-validator": "^2.0.3",
"ngx-push-notifications": "^7.0.2",
"rxjs": "~6.4.0",
"tslib": "^1.9.0",
"zone.js": "~0.9.1"
Expand Down
4 changes: 4 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class AppComponent implements OnInit, OnDestroy {
});
}
});
const isGranted = this.ladderService.notifications.isPermissionGranted;
if (!isGranted) {
this.ladderService.notifications.requestPermission();
}
}

ngOnDestroy() {
Expand Down
5 changes: 4 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { PushNotificationService } from 'ngx-push-notifications';

import { AmazingTimePickerModule } from 'amazing-time-picker';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatToolbarModule,
Expand Down Expand Up @@ -79,7 +82,7 @@ import { AuthInterceptor } from './auth/auth-interceptor';
MatProgressSpinnerModule,
MatSnackBarModule,
],
providers: [{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }],
providers: [{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }, PushNotificationService],
bootstrap: [AppComponent]
})
export class AppModule { }
42 changes: 41 additions & 1 deletion src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Profile } from 'selenium-webdriver/firefox';
import { MatSnackBar } from '@angular/material';
import { environment } from '../environments/environment';

import { PushNotificationOptions, PushNotificationService } from 'ngx-push-notifications';

const BackendURLNotifications = environment.apiUrl + 'notification/';
const BackendURLLadder = environment.apiUrl + 'table/';
const BackendURLChallenge = environment.apiUrl + 'challenge/';
Expand All @@ -29,12 +31,14 @@ export class LadderService {
private sportsUpdate = new Subject<string[]>();

private challengesN: number;
private challengesNOld: number;
private challengesUpdatesN = new Subject<number>();

private challengesP: number;
private challengesUpdatesP = new Subject<number>();

private challengesC: number;
private challengesCOld: number;
private challengesUpdatesC = new Subject<number>();


Expand All @@ -56,15 +60,26 @@ export class LadderService {
private player1: boolean;
private player1Sub = new Subject<boolean>();

constructor(private http: HttpClient, private router: Router, private snackBar: MatSnackBar) {}
constructor(
private http: HttpClient,
private router: Router,
private snackBar: MatSnackBar,
public notifications: PushNotificationService
) { }

// Notifications
getNumberChallenge(id: string) {
const myId = { id };
this.http.post<string>(BackendURLNotifications + 'challengesN/', myId)
.subscribe((notification) => {
this.challengesN = Number(notification);
// tslint:disable: triple-equals
if (this.challengesN != this.challengesNOld && this.challengesNOld != undefined) {
this.myNotifi('New Challenge', 'You got a new challenge!', '/challenges');
}
this.challengesUpdatesN.next(this.challengesN);
this.challengesNOld = this.challengesN;

});
}

Expand All @@ -90,7 +105,11 @@ export class LadderService {
this.http.post<string>(BackendURLNotifications + 'challengesC/', myId)
.subscribe((notification) => {
this.challengesC = Number(notification);
if (this.challengesC != this.challengesCOld && this.challengesCOld != undefined) {
this.myNotifi('Confirm Result', 'You got a new confirmation!', '/confirmation/confirm');
}
this.challengesUpdatesC.next(this.challengesC);
this.challengesCOld = this.challengesC;
});
}

Expand Down Expand Up @@ -336,4 +355,25 @@ export class LadderService {
this.snackBar.open(message, action, { duration: 2000 });
}

// Notifications Bitches!!!
myNotifi(title: string, body: string, link: string) {
const options = new PushNotificationOptions();
options.body = body;

this.notifications.create(title, options).subscribe((notif) => {
if (notif.event.type === 'show') {
setTimeout(() => {
notif.notification.close();
}, 10000);
}
if (notif.event.type === 'click') {
this.router.navigate([link]);
notif.notification.close();
}
},
(err) => {
console.log(err);
});
}

}

0 comments on commit 3a4cbd4

Please sign in to comment.