OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
606 stars 204 forks source link

[Bug]: OneSignal.Notifications.addForegroundWillDisplayListener can be executed multiple times #836

Closed IrfanKnowledge closed 5 months ago

IrfanKnowledge commented 5 months ago

What happened?

OneSignal.Notifications.addForegroundWillDisplayListener can be executed multiple times.

Everytime I enter page with OneSignal.Notifications.addForegroundWillDisplayListener placed inside void initState() {}, it will add the amount of executed everytime notification enter my android phone.

Steps to reproduce?

1. Install onesignal_flutter: ^5.1.0 to your flutter project.
2. Follow this guide [https://documentation.onesignal.com/docs/flutter-sdk-setup] to set up OneSignal Flutter SDK (for Android only)
3. Use this code [https://pub.dev/packages/onesignal_flutter/example]
4. remove setState((){}) code from OneSignal.Notifications.addForegroundWillDisplayListener((event){})

What did you expect to happen?

OneSignal.Notifications.addForegroundWillDisplayListener((event){}) should executed 1 time only.

OneSignal Flutter SDK version

onesignal_flutter: ^5.1.0

Which platform(s) are affected?

Relevant log output

No response

Code of Conduct

IrfanKnowledge commented 5 months ago

I just want to share that the current solution is just don't remove that setState(() {}).

halfmoon-mind commented 4 weeks ago

Hi Is there solution for this issue? I am experiencing the same problem. I added addForegroundWillDisplayListener and addClickListener when first init my app. But listener will continue to be added multiple times every time starting my app. Is there a good way to ensure that there is only 1 listener?

IrfanKnowledge commented 4 weeks ago

Hi Is there solution for this issue? I am experiencing the same problem. I added addForegroundWillDisplayListener and addClickListener when first init my app. But listener will continue to be added multiple times every time starting my app. Is there a good way to ensure that there is only 1 listener?

From the onesignal_flutter example, we can see setState() is used to change the string. But its main purpose is actually not to change the string from the example. Its main purpose is to prevent multiple listeners. I got this conclusion after still using setState() in my code.