I have been developing an Ionic app and incorporating local notifications with server-sent events from a Spring Boot backend. However, I am encountering issues when trying to call a web service every time a notification is clicked. The web service endpoint I am attempting to reach is localhost:8888/listtick.
import { Component, OnInit } from '@angular/core';
import {Observable} from "rxjs";
import { Platform, AlertController } from '@ionic/angular';
import { LocalNotifications, ELocalNotificationTriggerUnit, ILocalNotificationActionType, ILocalNotification } from '@ionic-native/local-notifications/ngx';
import { Alert } from 'selenium-webdriver';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
scheduled = [];
myData:any;
constructor(public http: HttpClient,private plt: Platform, private localNotifications: LocalNotifications, private alertCtrl: AlertController) {
this.connect();
this.plt.ready().then(() => {
this.localNotifications.on('click').subscribe(
res => {
let msg = res.data ? res.data.mydata : '';
this.showAlert(res.title, res.text, msg);
});
this.localNotifications.on('trigger').subscribe(res => {
let msg = res.data ? res.data.mydata : '';
this.showAlert(res.title, res.text, msg);
});
});
}
getListTickets(){
console.log('You will see this message every second');
var url='http://localhost:8888/listtick';
console.log("Clickeed");
var result = this.http.get(url);
return result;
}
connect(): void { //Get Notif From serverSentEvents
let source = new EventSource('http://localhost:8080/get_mydata');
source.addEventListener('message', message => {
this.myData = JSON.parse(message.data);
//alert(this.myData.data);
this.localNotifications.schedule({
id: 1,
title: 'Attention',
text: 'Test Notification',
data: { mydata: this.myData.data },
foreground: true // Show the notification while app is open
});
});
}
showAlert(header, sub, msg) {
this.alertCtrl.create({
header: header,
subHeader: sub,
message: msg,
buttons: ['Ok']
}).then(alert => alert.present());
}
}
The code above illustrates my current attempt. If anyone has suggestions or solutions, I would greatly appreciate the help!
Thank you in advance for any assistance provided.