Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question]: How to use NotificationWillDisplayEvent #1588

Closed
1 task done
serolgames opened this issue Oct 15, 2023 · 3 comments
Closed
1 task done

[question]: How to use NotificationWillDisplayEvent #1588

serolgames opened this issue Oct 15, 2023 · 3 comments

Comments

@serolgames
Copy link

serolgames commented Oct 15, 2023

How can we help?

Hello,

in older version we listened to incoming notification with
OneSignal.setNotificationWillShowInForegroundHandler

which is still in use in the official documentation...

But I found that we should use event listener NotificationWillDisplayEvent.

How to use it and read notification data ?

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jennantilla
Copy link
Contributor

jennantilla commented Oct 18, 2023

Hello @serolgames thanks for reaching out! The updated method is adding an event listener for foregroundWillDisplay.

Let us know if you have any additional questions or concerns.

Thanks!

@serolgames
Copy link
Author

Hello,

Thanks you !
It's working but I found another issue :(

I'm using the following code :

const onReceived = (event: any) => 
{                    
    event.preventDefault()
    const additionalData = event.getNotification().additionalData

    if(/*my logic to display only if I want*/)
    {
        event.getNotification().display();
    }
}

useEffect(() =>
{
    OneSignal.Notifications.addEventListener('foregroundWillDisplay', onReceived);

    return () => {
        console.log("removing event...")
        OneSignal.Notifications.removeEventListener('foregroundWillDisplay', onReceived);
    };
}, []);

So basically, it's working. BUT, the event seems to never be removed, I get the log "removing event..." so removeEventListener is properly called but it's never getting removed. Is it a known issue ? Or a problem in my code ?

@mathbalduino
Copy link

It's still happening

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants