RemoveNotificationListener is returning error

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]);

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

Screenshot 2023-12-01 at 18.48.33

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>