From 35e2593aef8be2e0fa5523f1354c62b09cfc0e6b Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Sat, 28 Nov 2020 19:33:36 +0000 Subject: [PATCH] Add device token management --- package-lock.json | 9 +++++--- .../send-push-notification.component.html | 8 +++++++ .../send-push-notification.component.ts | 14 ++++++++++++ src/app/providers/api-service.ts | 22 +++++++++++-------- src/app/service/messaging.service.ts | 4 ++-- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4c9f5ce..6d34df4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4743,7 +4743,8 @@ }, "kind-of": { "version": "6.0.2", - "resolved": "" + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" } } }, @@ -7570,7 +7571,8 @@ }, "kind-of": { "version": "6.0.2", - "resolved": "" + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" } } }, @@ -15021,7 +15023,8 @@ }, "kind-of": { "version": "6.0.2", - "resolved": "" + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" } } }, diff --git a/src/app/dashboard/send-push-notification.component.html b/src/app/dashboard/send-push-notification.component.html index 326eb74..14f70dd 100644 --- a/src/app/dashboard/send-push-notification.component.html +++ b/src/app/dashboard/send-push-notification.component.html @@ -8,6 +8,14 @@
+
+ + +
diff --git a/src/app/dashboard/send-push-notification.component.ts b/src/app/dashboard/send-push-notification.component.ts index 024d2fd..868b966 100644 --- a/src/app/dashboard/send-push-notification.component.ts +++ b/src/app/dashboard/send-push-notification.component.ts @@ -13,10 +13,24 @@ export class SendPushNotificationComponent implements OnInit { noEmail = false; sendMessageFormStatus: any; sendMessageFormStatusError = 'Error received, please try again.'; + deviceTokenList: any; + deviceTokenIdList: any; constructor(private api: ApiService) { + this.api.getDeviceTokens().subscribe( + result => { + console.log(result); + this.deviceTokenList = result.tokens; + this.deviceTokenIdList = Object.keys(this.deviceTokenList); + }, + error => { + console.log("Couldn't get device token"); + console.log(error._body); + } + ); this.sendMessageForm = new FormGroup({ messagetext: new FormControl('', Validators.required), + devicetokens: new FormControl('', Validators.required), }); } diff --git a/src/app/providers/api-service.ts b/src/app/providers/api-service.ts index b2cbc80..dd398e0 100644 --- a/src/app/providers/api-service.ts +++ b/src/app/providers/api-service.ts @@ -3,7 +3,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { environment } from '../../environments/environment'; - /* this provider handles the interaction between server and client */ @Injectable() @@ -102,18 +101,23 @@ export class ApiService { // Push notifications public addDeviceToken(data) { - const key = this.sessionKey; + data.session_key = this.sessionKey; return this.http.post( this.apiUrl + '/add-device-token', - { - session_key: key, - token: data - } + data + ); + } + + public getDeviceTokens() { + const key = this.sessionKey; + return this.http.post( + this.apiUrl + '/get-device-tokens', + { session_key : key } ); } public sendMessage(data) { - data.devicetoken = environment.deviceToken; + data.devicetoken = localStorage.getItem('devicetoken'); data.sender = localStorage.getItem('displayname'); return this.http.post( this.apiUrl + '/send-message', @@ -358,8 +362,8 @@ export class ApiService { public getMapData(data) { data.session_key = this.sessionKey; return this.http.post( - this.apiUrl + '/v1/supplier/location', - data + this.apiUrl + '/v1/supplier/location', + data ); } diff --git a/src/app/service/messaging.service.ts b/src/app/service/messaging.service.ts index 200a1bb..cd00656 100644 --- a/src/app/service/messaging.service.ts +++ b/src/app/service/messaging.service.ts @@ -21,10 +21,10 @@ export class MessagingService { requestPermission() { this.angularFireMessaging.requestToken.subscribe((token) => { console.log("Device token: " + token); - return - this.api.addDeviceToken(token).subscribe( + this.api.addDeviceToken({'token': token, 'email': localStorage.getItem('email')}).subscribe( result => { console.log("Device registered successfully!"); + localStorage.setItem('devicetoken', token); }, error => { console.error("Device could not be registered!", error._body);