My stack is Ionic + Vue (v. 5.4.0) and Capacitor (v. 2.4.7). I’m having trouble getting the Local Notifications to trigger properly.
Here’s a slightly simplified version of my code:
await LocalNotifications.requestPermission();
await LocalNotifications.schedule({
notifications: [{
title: 'TESTING',
body: 'TEST',
id: 1,
schedule: {
at: new Date(Date.now() + 2000), // Schedule this for 2 seconds from now
repeats: false
},
autoCancel: true
}]
});
You’ll notice that I’m requesting permission to show Local Notifications just prior to showing one. The problem is it seems that I am unable to schedule notifications during the session in which I authorizer permission for the notifications.
However, if I close the app and open it again, the notifications work just fine.
To add to the confusion: if I schedule a notification for the current time instead of some time in the future, it will show a notification (but only that particular notification) as soon as I press the “Allow” button on the permission request modal:
await LocalNotifications.requestPermission();
await LocalNotifications.schedule({
notifications: [{
title: 'TESTING',
body: 'TEST',
id: 1,
schedule: {
at: new Date(), // Notice that I'm not modifying this date
repeats: false
},
autoCancel: true
}]
});
Although, as a side note I do see an error in the console that indicates that messages should be scheduled in the future:
ERROR MESSAGE: {“message”:“Scheduled time must be after current time”,“errorMessage”:“”}
I’ve tested this on XCode emulator using both iOS version 12.x and iOS version 15.x