Add device token management

This commit is contained in:
Ben Goldsworthy 2020-11-28 19:33:36 +00:00
parent e25ccfaff5
commit 35e2593aef
5 changed files with 43 additions and 14 deletions

9
package-lock.json generated
View file

@ -4743,7 +4743,8 @@
}, },
"kind-of": { "kind-of": {
"version": "6.0.2", "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": { "kind-of": {
"version": "6.0.2", "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": { "kind-of": {
"version": "6.0.2", "version": "6.0.2",
"resolved": "" "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz",
"integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA=="
} }
} }
}, },

View file

@ -8,6 +8,14 @@
</div> </div>
<form class="form-horizontal" [formGroup]="sendMessageForm" (ngSubmit)="onSubmit()"> <form class="form-horizontal" [formGroup]="sendMessageForm" (ngSubmit)="onSubmit()">
<div class="card-block"> <div class="card-block">
<div class="form-group row">
<label class="col-md-3 form-control-label" for="select-input"><strong>Select Recipients</strong></label>
<select required class="form-control" type="text" formControlName="devicetokens" multiple>
<option *ngFor="let deviceToken of deviceTokenIdList" [ngValue]="deviceTokenList[deviceToken].token">
{{ deviceTokenList[deviceToken].user }} <{{deviceTokenList[deviceToken].token}}>
</option>
</select>
</div>
<div class="form-group row"> <div class="form-group row">
<label class="col-md-3 form-control-label" for="text-input"><strong>Enter Message Here</strong></label> <label class="col-md-3 form-control-label" for="text-input"><strong>Enter Message Here</strong></label>
<div class="col-md-9"> <div class="col-md-9">

View file

@ -13,10 +13,24 @@ export class SendPushNotificationComponent implements OnInit {
noEmail = false; noEmail = false;
sendMessageFormStatus: any; sendMessageFormStatus: any;
sendMessageFormStatusError = 'Error received, please try again.'; sendMessageFormStatusError = 'Error received, please try again.';
deviceTokenList: any;
deviceTokenIdList: any;
constructor(private api: ApiService) { 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({ this.sendMessageForm = new FormGroup({
messagetext: new FormControl('', Validators.required), messagetext: new FormControl('', Validators.required),
devicetokens: new FormControl('', Validators.required),
}); });
} }

View file

@ -3,7 +3,6 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
/* this provider handles the interaction between server and client */ /* this provider handles the interaction between server and client */
@Injectable() @Injectable()
@ -102,18 +101,23 @@ export class ApiService {
// Push notifications // Push notifications
public addDeviceToken(data) { public addDeviceToken(data) {
const key = this.sessionKey; data.session_key = this.sessionKey;
return this.http.post<any>( return this.http.post<any>(
this.apiUrl + '/add-device-token', this.apiUrl + '/add-device-token',
{ data
session_key: key, );
token: data
} }
public getDeviceTokens() {
const key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/get-device-tokens',
{ session_key : key }
); );
} }
public sendMessage(data) { public sendMessage(data) {
data.devicetoken = environment.deviceToken; data.devicetoken = localStorage.getItem('devicetoken');
data.sender = localStorage.getItem('displayname'); data.sender = localStorage.getItem('displayname');
return this.http.post<any>( return this.http.post<any>(
this.apiUrl + '/send-message', this.apiUrl + '/send-message',

View file

@ -21,10 +21,10 @@ export class MessagingService {
requestPermission() { requestPermission() {
this.angularFireMessaging.requestToken.subscribe((token) => { this.angularFireMessaging.requestToken.subscribe((token) => {
console.log("Device token: " + token); console.log("Device token: " + token);
return this.api.addDeviceToken({'token': token, 'email': localStorage.getItem('email')}).subscribe(
this.api.addDeviceToken(token).subscribe(
result => { result => {
console.log("Device registered successfully!"); console.log("Device registered successfully!");
localStorage.setItem('devicetoken', token);
}, },
error => { error => {
console.error("Device could not be registered!", error._body); console.error("Device could not be registered!", error._body);