Add push notification and device token management.
This commit is contained in:
parent
a829f73e6d
commit
854571da6e
6 changed files with 74 additions and 30 deletions
|
@ -1 +1,4 @@
|
||||||
{{ message | async | json }}
|
<router-outlet></router-outlet>
|
||||||
|
<div *ngIf="userType === 'customer' && message.value" style="position: fixed; padding:1em; right: 2em; bottom: 2em; background-color: grey;">
|
||||||
|
{{ message | async | json }}
|
||||||
|
</div>
|
||||||
|
|
|
@ -1,21 +1,25 @@
|
||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { MessagingService } from './service/messaging.service';
|
import { MessagingService } from './service/messaging.service';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
template: '<router-outlet></router-outlet>'
|
templateUrl: './app.component.html',
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
title = 'push-notification';
|
title = 'push-notification';
|
||||||
message;
|
message;
|
||||||
|
userType;
|
||||||
|
|
||||||
constructor(private messagingService: MessagingService) { }
|
constructor(private messagingService: MessagingService) { }
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
if (localStorage.getItem('usertype') === 'customer') {
|
this.userType = localStorage.getItem('usertype');
|
||||||
|
if (this.userType === 'customer') {
|
||||||
this.messagingService.requestPermission();
|
this.messagingService.requestPermission();
|
||||||
this.messagingService.receiveMessage();
|
this.messagingService.receiveMessage();
|
||||||
this.message = this.messagingService.currentMessage;
|
this.message = this.messagingService.currentMessage;
|
||||||
}
|
}
|
||||||
|
console.log(this.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
<div class="card-block">
|
<div class="card-block">
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-md-3 form-control-label" for="select-input"><strong>Select Recipients</strong></label>
|
<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>
|
<select required class="form-control" type="text" formControlName="topic">
|
||||||
<option *ngFor="let deviceToken of deviceTokenIdList" [ngValue]="deviceTokenList[deviceToken].token">
|
<option *ngFor="let topic of topicIdList" [ngValue]="topicList[topic].name">
|
||||||
{{ deviceTokenList[deviceToken].user }} <{{deviceTokenList[deviceToken].token}}>
|
{{ topicList[topic].name }} <strong>({{topicList[topic].numberOfSubscribers}} subscribers)</strong>
|
||||||
</option>
|
</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,24 +13,23 @@ 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;
|
topicList: any;
|
||||||
deviceTokenIdList: any;
|
topicIdList: any;
|
||||||
|
|
||||||
constructor(private api: ApiService) {
|
constructor(private api: ApiService) {
|
||||||
this.api.getDeviceTokens().subscribe(
|
this.api.getTopics().subscribe(
|
||||||
result => {
|
result => {
|
||||||
console.log(result);
|
this.topicList = result.topics;
|
||||||
this.deviceTokenList = result.tokens;
|
this.topicIdList = Object.keys(this.topicList);
|
||||||
this.deviceTokenIdList = Object.keys(this.deviceTokenList);
|
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
console.log("Couldn't get device token");
|
console.log("Couldn't get topics");
|
||||||
console.log(error._body);
|
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),
|
topic: new FormControl('', Validators.required),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +57,6 @@ export class SendPushNotificationComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
onSubmit(): void {
|
onSubmit(): void {
|
||||||
console.log(this.sendMessageForm.value);
|
|
||||||
this.api
|
this.api
|
||||||
.sendMessage(this.sendMessageForm.value)
|
.sendMessage(this.sendMessageForm.value)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
|
|
|
@ -108,6 +108,14 @@ export class ApiService {
|
||||||
|
|
||||||
// Push notifications
|
// Push notifications
|
||||||
|
|
||||||
|
public checkDeviceToken(data) {
|
||||||
|
data.session_key = this.sessionKey;
|
||||||
|
return this.http.post<any>(
|
||||||
|
this.apiUrl + '/check-device-token',
|
||||||
|
data
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public addDeviceToken(data) {
|
public addDeviceToken(data) {
|
||||||
data.session_key = this.sessionKey;
|
data.session_key = this.sessionKey;
|
||||||
return this.http.post<any>(
|
return this.http.post<any>(
|
||||||
|
@ -124,8 +132,15 @@ export class ApiService {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getTopics() {
|
||||||
|
const key = this.sessionKey;
|
||||||
|
return this.http.post<any>(
|
||||||
|
this.apiUrl + '/get-topics',
|
||||||
|
{ session_key : key }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public sendMessage(data) {
|
public sendMessage(data) {
|
||||||
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',
|
||||||
|
|
|
@ -9,17 +9,35 @@ export class MessagingService {
|
||||||
currentMessage = new BehaviorSubject(null);
|
currentMessage = new BehaviorSubject(null);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private angularFireMessaging: AngularFireMessaging,
|
private readonly angularFireMessaging: AngularFireMessaging,
|
||||||
private api: ApiService
|
private api: ApiService
|
||||||
) {
|
) { }
|
||||||
this.angularFireMessaging.messages.subscribe((_messaging: AngularFireMessaging) => {
|
|
||||||
_messaging.onMessage = _messaging.onMessage.bind(_messaging);
|
|
||||||
_messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
requestPermission() {
|
requestPermission() {
|
||||||
|
if (!('Notification' in window)) {
|
||||||
|
alert("This browser does not support notifications.");
|
||||||
|
return;
|
||||||
|
} else if (Notification.permission === 'granted') {
|
||||||
|
console.log("Push notification permission granted");
|
||||||
|
this.registerDeviceToken();
|
||||||
|
} else if (Notification.permission === 'denied' ||
|
||||||
|
Notification.permission === 'default') {
|
||||||
|
console.log("Push notification permission not granted");
|
||||||
|
Notification.requestPermission().then(function(permission) {
|
||||||
|
if (permission === 'granted') {
|
||||||
|
console.log("Push notification permission granted");
|
||||||
|
this.registerDeviceToken();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registerDeviceToken() {
|
||||||
this.angularFireMessaging.requestToken.subscribe((token) => {
|
this.angularFireMessaging.requestToken.subscribe((token) => {
|
||||||
|
this.api.checkDeviceToken({'token': token}).subscribe(
|
||||||
|
result => {
|
||||||
|
if (result.exists) console.log("Device already registered!");
|
||||||
|
else {
|
||||||
this.api.addDeviceToken({'token': token, 'email': localStorage.getItem('email')}).subscribe(
|
this.api.addDeviceToken({'token': token, 'email': localStorage.getItem('email')}).subscribe(
|
||||||
result => {
|
result => {
|
||||||
console.log("Device registered successfully!");
|
console.log("Device registered successfully!");
|
||||||
|
@ -29,15 +47,21 @@ export class MessagingService {
|
||||||
console.error("Device could not be registered!", error._body);
|
console.error("Device could not be registered!", error._body);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
console.error(error._body);
|
||||||
|
}
|
||||||
|
);
|
||||||
}, (err) => {
|
}, (err) => {
|
||||||
console.error('Unable to get permission to notify.', err);
|
console.error('Unable to get permission to notify.', err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
receiveMessage() {
|
receiveMessage() {
|
||||||
this.angularFireMessaging.messages.subscribe((payload) => {
|
this.angularFireMessaging.messages.subscribe((message) => {
|
||||||
console.log("new message received. ", payload);
|
console.log("show message!", message);
|
||||||
this.currentMessage.next(payload);
|
this.currentMessage.next(message);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue