Hi!
Maybe you figured this out already but it is something I was struggling with for some time. The only way I found to do this was to use the setInterval() to call the Local Notifications scheduling each day.
Just want to clarify, I’m a Hobby programmer so my code is not elegant
Within the Scheduling function, I remove the Listeners, update the title and body strings, then re-add the Listeners, and call LocalNotifications.schedule again.
It should also be noted that you will need to setup Deeplinks correctly to use the “this.router.navigateByUrl(‘/display-notification/’ + qID);” in the localNotificationActionPerformed Listener.
async ngOnInit() {
this.Questions = AppComponent.Questions;
this.Descriptions = AppComponent.Details;
await LocalNotifications.requestPermissions();
LocalNotifications.registerActionTypes({
types: [
{
id: 'REMINDERS',
actions: [
{
id: 'open',
title: 'Open App'
},
{
id: 'remove',
title: 'Clear Message',
destructive: true
}
]
}
]
});
await this.checkReminderState();
if (this.reminder) {
await this.scheduleReminder();
}
setInterval(() => {
// For testing purposes this is currently set every 60 second -
if (this.reminder) {
this.scheduleReminder();
}
}, 60000);
}
async checkReminderState() {
…
// Set reminder value true or false
}
async scheduleReminder() {
// Remove Listeners
await LocalNotifications.removeAllListeners();
await LocalNotifications.cancel({ notifications: [{ id: 1 }] });
this.setQuestion(); // Update the values of title and body (this.notificationDetails.title, this.notificationDetails.body,)
this.addNotificationListeners(); // Re-add the Listeners
await LocalNotifications.schedule({
notifications: [
{
title: this.notificationDetails.title,
body: this.notificationDetails.body,
id: 1,
extra: {
data: this.notificationDetails.id,
},
iconColor: 'green',
actionTypeId: 'REMINDERS',
schedule: {
on: {
// hour: 10,
second: 10,
},
every: 'day',
allowWhileIdle: true,
},
},
],
});
}
addNotificationListeners() {
const qID = this.notificationDetails.id;
LocalNotifications.addListener('localNotificationActionPerformed', (notification) => {
//Capturing the actionId for validation
if (notification.actionId) {
console.log('Local Notification Action ID: ' + notification.actionId);
}
if (notification.actionId === 'tap') {
this.zone.run(() => {
this.router.navigateByUrl('/display-notification/' + qID);
});
}
else if (notification.actionId === 'open') {
this.zone.run(() => {
this.router.navigateByUrl('/display-notification/' + qID);
});
}
});
LocalNotifications.addListener('localNotificationReceived', (notification) => {
});
}