Hi,
This code snippet is returning “Error: Notifications could not be registered at removeNotifications” when I try to remove notification listeners. Does anyone know why?
const events = ['open'];
const notificationListener = (e, d) => {
console.log(e, d);
};
const removeNotifications = () => {
try {
action.removeNotificationListener(events, notificationListener);
} catch (e) {
console.log(e);
}
};
const addNotifications = () => {
try {
action.addNotificationListener(events, notificationListener);
} catch (e) {
console.log(e);
}
};
useEffect(() => {
if (isListening) {
addNotifications();
} else {
removeNotifications();
}
}, [isListening]);
Jarda
December 1, 2023, 5:24pm
2
Because there are no listeners already? It should only work when you add them.
As far as I know is registered. Is returning open event, but when I try to remove, it doesn’t work
Jarda
December 1, 2023, 7:14pm
4
Isn’t action.addNotificationListener
returning promise? Maybe you call it way too quickly? Or more than you should?
If I read it correctly, you addNotifications
only when isListening
is set to true
. When and where do you do that?
The addNotification is not returning a promise, is returning a “undefined”.
The variable isListening is from a useState. And only is enabling/disabling with a button interface
const [isListening, setIsListening] = useState(false);
useEffect(() => {
if (isListening) {
const result = action.addNotificationListener(events, notificationListener);
console.log(result);
} else if (!isListening && !firstTime) {
const result = action.removeNotificationListener(events, notificationListener);
console.log(result);
}
}, [isListening]);
<sp-action-button
quiet
onClick={() => {
setIsListening(!isListening);
}}>
{isListening ? <StopIcon /> : <StartIcon />}
</sp-action-button>