Reputation: 339
I'm developing an app using Ionic, Capacitor and Angular.
This project uses Local Notifications of Capacitor (https://capacitor.ionicframework.com/docs/apis/local-Notifications/)
I now how to cancel all the local notifications at the same time, but I need to cancel only one using its id but I don't know how to do it.
I have read the official documentation but I don't find how to do it. Can somebody help me?
Thank you,
Upvotes: 3
Views: 3391
Reputation: 31
Just wait for the platform is ready and cancel all...
this.platform.ready().then()
{
// Cancel All
this.localNotifications.cancelAll();
// Cancel by ID
this.localNotifications.cancel(1);
}
Upvotes: 0
Reputation: 452
Use the below function to cancel and clear a single notification from the list.
cancel_notification(){
LocalNotifications.getPending().then( res => {
res.notifications = res.notifications.filter(i => i.id == this.notificationId);
if(res.notifications.length>0)
LocalNotifications.cancel(res);
else
{
console.log('notification length zero');
}
}, err => {
console.log(err);
})
}
Upvotes: 1
Reputation: 721
stumbled upon exactly the same issue. I created a workaround by converting my unique string guid to a numeric hash, which seems to work for my purpose now.
Produces a persistent hashcode for a string
// convert a string to hashcode (integer)
private stringHashCode = (str) => {
let hash = 0;
for (let i = 0; i < str.length; ++i) {
hash = (Math.imul(31, hash) + str.charCodeAt(i)) | 0;
}
return hash;
}
The notificationId is the unique database-key (session.id) converted to a numeric hashcode
// make guid numeric
const notificationId = this.stringHashCode(session.id);
// schedule logic
LocalNotifications.schedule({
notifications: [{
title: 'test',
body: 'body content',
id: notificationId, // <== numeric
schedule: { at: moment().add(10, 'seconds').toDate() },
}]
});
// cancel logic
const notifications: LocalNotificationRequest[] = [{ id : `${notificationId}`}]; // <== string
await LocalNotifications.cancel({notifications});
Upvotes: 1
Reputation: 770
The solution provided by did not work for me
After hours of spending trying to solve the same problem, I have managed to come up with simpler solution. Just create a new object of pending notifications to cancel.
const pending: LocalNotificationPendingList = {
notifications: [
{
id: 'reminderId', // must be a string
},
],
};
return LocalNotifications.cancel(pending);
Upvotes: 2
Reputation: 339
I have found a solution. As said Mostafa I got all the pending notifications and then I have an array with all its ids.
With these ids and splice I can modify the array removing all the notifications that I don't want to delete and after that I cancel all the notifications of the array.
I leave here the method that I'm using in this case to delete all te notifcations except the notification con id 10000000.
LocalNotifications.getPending().then( res => {
var index = res.notifications.map(x => {
return x["id"];
}).indexOf("10000000");
res.notifications.splice(index, 1);
LocalNotifications.cancel(res);
}, err => {
console.log(err);
})
Upvotes: 2