-
Notifications
You must be signed in to change notification settings - Fork 223
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
[Bug]: OneSignal#onWillDisplayNotification getting called as many times as app gets hot restarted while debugging. #763
Comments
With The problem seems that on the native code side a List of listeners is kept, and this list remains the same on each hot-restart, so on each OneSignal initialization or adding click listener, the native list of listeners increases. |
@luis901101 Thank you for reporting we will investigate. In the meantime the corresponding removeListener most likely needs to be called to remove the old listener or you could try not adding the second listeners on hot reload |
Sure, the remove listener should be properly called, but note in my description above I'm talking about Hot Restart not Hot Reload, and also about the OneSignal#onWillDisplayNotification it doesn't matter the listener, I mean, it will be called X times depending on the amount of X times you Hot Restart the app, no matter if you added a listener or not, because it seems that on native implementation side the listeners list are being added on every |
The call to |
@luis901101 I see thank you for investigating! We will work on a fix for this |
I been stuck on this issue for a few days now, and i couldn't quite reproduce it until now. Have you got a temporary workaround for this? @luis901101 |
Nope, I just avoid onesignal initialization during debug unless I need to test something related to notifications which is sporadical. |
+1 I am also experiencing this issue |
+1 |
3 similar comments
+1 |
+1 |
+1 |
facing same issue |
same here |
Please help any one!!! i used one signal 5.0.4 version, adclicklistener lisent multiple times. how to handle this ? |
Is there any ETA to when this issue is going to get resolved? Also will this affect the "release" version of flutter app or only debug? |
Same |
EDIT: I meant Hot Restart in this comment Hi everyone, thanks for your reports. The OneSignal-Flutter-SDK does support adding multiple listeners of each type. After investigating, this seems like a Flutter Hot Reload issue, and I am not sure how the SDK can work around it without impacting real usage. When you run a Hot Reload, it doesn't cold start the app but still reruns your initialization code, which is why your listeners are added again. Just put a breakpoint or print statement in the place where you are adding your listener and you will see the Hot Reload does call your code again to add the listeners. Because it is not a cold start, all of the SDK's data remains in memory and it cannot distinguish between a Hot Reload vs. normal app flow or backgrounding, etc. The SDK cannot distinguish that you are adding a listener for real or Hot Reload is adding the listener. If you have suggestions how to work around this for easier debugging, please let us know. It doesn't seem the SDK can make this fix. |
Hi @nan-li and thanks for your feedback, but note that in my first comment I state Hot Restarts not Hot Reloads
So the way I see it, if the app is Hot Restarted the |
I'm encountering this issue as well and I think resetting all listeners on native side when calling |
Hi @luis901101, I apologize, I misspoke in my comment, I meant to say Hot Restart as that is the flow I tested. I disagree that calling
There are some methods that could be called before |
Hi @nan-li thanks again for you feedback and your tests, oki then we are on the same page with the Hot Restart (by the way you mentioned Hot Reloads again at the end of your comment 😉) Agree, resetting the whole plugin state is not ideal, but resetting only the listeners I think it makes sense and in fact only the listener are the ones with the problems mentioned in this thread so no need to reset anything else. From your sample code I understand the |
Thanks for the reply. I understand that changing the behavior in such way is not ideal, even though I myself wouldn't think of doing anything before initialization. What about a method that clears all listeners in both Flutter and native side? Keeping track of the function references for their removal is annoying anyway. |
Hi @luis901101 oops yes you are right, I just can't stop saying Hot Reloads 😆. The thing is we would like to avoid any "gotchas" that SDK users have to watch for. If they have complex code and already called I also recall on our previous major release with the player-based model, some people's notification opened handlers didn't trigger early enough when the app is cold started from the open event. We had suggested to some users to add the opened handler as early as possible. In addition, clearing listeners on @PetrKubes97 You should manage the removal and addition of listeners when debugging only. Perhaps call the |
@nan-li sorry if this comment is inaccurate, I'm trying to remember the code and typing it on a phone 😅. From what I remember, the remove method only removes the listener from the list of listeners in flutter with native listeners still being registered. What happens then is that there are two native listeners, which call the flutter listener twice (even though there is only one in the list) |
Ok let's break this into parts:
Follow this simple test case carefully (so far I only tested it on iOS):
If you hot restart your app 5 times, when you send the test notification to your device, you will see the
|
@nan-li Hi Nan, First, I'd like to say that I appreciate the work you and the OneSignal team are doing! I'd like to address your comment when you said, 'we would like to avoid any "gotchas" that SDK users have to watch for' and then proceeded to explain the reasoning behind allowing behaviors such as duplicate initialization calls or allowing hooks to be set before initialization is called. Would it not be a "gotcha" that people will probably expect that calling By allowing the API to be used in erroneous ways like disregarding when initialized is called, ignoring duplicate initialize calls, etc, you actually significantly worsen the developer experience. For example, if someone is unintentionally calling This is akin to what JavaScript does when it allows users to do I apologize for being a little dogmatic, but I would really appreciate some concrete reasons behind why What if there were a happy medium where |
Maybe a quick fix: I think it could be done more declaratively, checking whether listeners are registered rather than checking some flag. The current bug is caused by the mismatch between the flags state and the registered listeners state. |
same issue on onesignal 5.2, this anybody have solved this problem ? |
Have the same issue. Onesignal SDK = onesignal_flutter: ^5.1.0
|
Following, and hoping for a proper fix from OneSignal. In the meantime we have added a debounce, here is a basic template, if it helps anyone here:
|
I appear to be experiencing this issue in release mode as well. A notification that pushes a navigation route gets pushed twice when opened. |
Did you find the solution? I am having the same issue but I didn't find a solution of why it is doing like that? |
Hi @Ibrar-Atrule the "why the plugin is doing like that" in my opinion is because wrong handling of listeners initialization on native side, I mention it in my previous comments... until someone make a PR with the fix, what you can do is to handle the issue with a debounce strategy like the one described in this comment #763 (comment), for the Here some example as an idea... void startListeners() {
final onReceiveListener = _handleOneSignalReceivedNotification;
OneSignal.Notifications.removeForegroundWillDisplayListener(onReceiveListener);
OneSignal.Notifications.addForegroundWillDisplayListener(onReceiveListener);
final onClickListener = _handleOneSignalClickNotification;
OneSignal.Notifications.removeClickListener(onClickListener);
OneSignal.Notifications.addClickListener(onClickListener);
}
static int lastNotificationReceivedTime = 0;
void _handleOneSignalReceivedNotification(OSNotificationWillDisplayEvent event) {
try {
//FIXME: remove this code when OneSignal fixes the issue: https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/763
final now = DateTime.now().microsecondsSinceEpoch;
if (now - lastNotificationReceivedTime < 1000) {
event.preventDefault();
return;
}
// If function gets here the notification will be displayed
} catch (e) {
print(e);
}
}
static int lastNotificationClickedTime = 0;
void _handleOneSignalClickNotification(OSNotificationClickEvent event) {
try {
//FIXME: remove this code when OneSignal fixes the issue: https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/763
final now = DateTime.now().microsecondsSinceEpoch;
if (now - lastNotificationClickedTime < 1000) return;
final Map<String, dynamic>? payloadAdditionalData = event.notification.additionalData;
openConentFromNotification(payloadAdditionalData);
} catch (e) {
print(e);
}
} |
Issue DescriptionI've encountered a recurring problem with the OneSignal Flutter SDK where listeners are triggered repeatedly and increasingly more frequently with each execution. This issue was first reported back in 2023, and as of September 2024, it still persists. Proposed SolutionTo resolve this, I implemented a solution that tracks the last processed notification ID and prevents the listener from executing multiple times for the same notification. Here’s how I achieved it step by step:
|
I also have done it through its notification ID, I have created a set when I have stored the notification ID, that's how one notification can't be appeared multiple times, and it worked for me. |
Yes working . Thanks lot |
What happened?
The callback for
_handleMethod(MethodCall call)
function onOneSignalNotifications
class is getting called as many times as the app get's Hot Restarted, so listeners gets called several times times during debugging.Steps to reproduce?
What did you expect to happen?
The callback should be getting called just one time by notification event on each app hot restart.
OneSignal Flutter SDK version
5.0.3
Which platform(s) are affected?
Relevant log output
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: