Skip to content

[Bug]: Problem with addEventListener removeEventListenerΒ #1610

@serolgames

Description

@serolgames

What happened?

It seems that removeEventListener doesn't work as expected

I have the following code :

useEffect(() => 
{
    const eventMethod = (event: any) => 
    {
        console.log("Event listener called")
        let notification = event.getNotification();
        const data: any = notification.additionalData 

        if (data.refresh_messages == true) 
        {
            event.preventDefault();
        }
        else 
        {
            event.getNotification().display();
        }
    }

    OneSignal.Notifications.addEventListener('foregroundWillDisplay', eventMethod);

    return () => 
    {
        OneSignal.Notifications.removeEventListener('foregroundWillDisplay', eventMethod)
    };
}, []);

So when I'm on my page and I receive a notification my console says "Event listener called". When I leave the page I remove the event listener (with the return method of the useEffect) then when I receive a notification my console don't say "Event listener called". So... It's working ! But... no, because the notification is not shown. So in my opinion it seems that the removeEventListener does his work properly, but that the preventDefault() is still working because no notification with refresh_messages to true are displayed.

Steps to reproduce?

1. Install react native onesignal v5.0.3
2. Copy my code on another page (let's call it messagePage)
3. Go on your homePage and try to receive a notification (it will display)
4. Go on your messagePage and try to receive a notification (it won't display (as expected))
5. Go back to homePage and try to receive a notification (it won't display :( )

What did you expect to happen?

As I removed the listener. Then every notification should display

React Native OneSignal SDK version

5.0.3

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions