I have created a notification that is scheduled every minute just to test it. It’s working perfectly fine. I’m looking for a way to update the notification every time it is called making it show new data every time. I haven’t been able to figure out how to do it. I have tried a few different apporaches with the update but nothing seems to work.
I’m using Ionic 3 and Angular 4.
The testNotification function is the one in question. Here is my entire typescript for the page:
import { Component } from '@angular/core';
import { NavController, NavParams, Platform, AlertController } from 'ionic-angular';
import { LocalNotifications } from '@ionic-native/local-notifications';
// Service import for items
import { ItemApi } from '../../services/service';
@Component({
selector: 'page-notification',
templateUrl: 'notification.html',
})
export class NotificationPage {
// Define the variables used in the view
notifications: any[] = [];
items: any;
oneItem: any;
public buttonClicked: boolean = true;
// The constructor initializes the imports
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public platform: Platform,
public alertController: AlertController,
public localNotifications: LocalNotifications,
private itemApi: ItemApi
)
{}
// --------------------------------------------------------------------------------------
// ---- FUNCTIONS
// --------------------------------------------------------------------------------------
// This is where the data loads from the service.
// It happens when the view loads for the first time.
ionViewDidLoad() {
// Get the JSON data from our itemApi
this.itemApi.getItems().then(data => {
this.items = data;
let randomNumber = Math.floor(Math.random() * this.items.length)
// Select the random object an pass it to the oneItem object
this.oneItem = this.items[randomNumber];
});
}
// --------------------------------------------------------------------------------------
// ---- Main notification function
// --------------------------------------------------------------------------------------
testNotification() {
// The notification
let notification = {
id:1,
title: this.oneItem.word,
text: this.oneItem.description,
every: "minute"
};
this.notifications.push(notification);
if(this.platform.is('cordova')){
// Cancel any existing notifications
this.localNotifications.cancelAll().then(() => {
// Schedule the new notifications
this.localNotifications.schedule(this.notifications);
this.notifications = [];
let alert = this.alertController.create({
title: 'Notifications set',
buttons: ['Ok']
});
// Initializes the alert
alert.present().then(() => {
// Get the JSON data from our itemApi
this.itemApi.getItems().then(data => {
this.items = data;
let randomNumber = Math.floor(Math.random() * this.items.length)
// Select the random object an pass it to the oneItem object
this.oneItem = this.items[randomNumber];
});
// The notification is updated
this.localNotifications.update(notification = {
id:1,
title: this.oneItem.word,
text: this.oneItem.description,
every: "minute"
});
});
});
}
}
// End of notification
// --------------------------------------------------------------------------------------
// ---- CancelAll notifcations in a function.
// --------------------------------------------------------------------------------------
cancelAll(){
this.localNotifications.cancelAll();
let alert = this.alertController.create({
title: 'Notifications cancelled',
buttons: ['Ok']
});
alert.present();
}
// End of cancelAll function
// --------------------------------------------------------------------------------------
// ---- A function called on the back button to go back
// --------------------------------------------------------------------------------------
goBackButton(){
// Go back one view in the navigation stack
this.navCtrl.pop();
// Hide the element just when it is clicked
this.buttonClicked = !this.buttonClicked;
}
}
Do you have any idea how to solve this?
Thanks!