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

[Bug]: Possibly threading issue with willDisplay on Android + documentation clarification #1672

Open
2 of 3 tasks
samsnori opened this issue Mar 23, 2024 · 0 comments
Open
2 of 3 tasks

Comments

@samsnori
Copy link

What happened?

I'm using the OneSignal SDK to handle notifications in my React Native mobile app, compatible with both iOS and Android. Currently, I'm encountering an issue while running my app in development mode via the Metro Bundler: the foregroundWillDisplay event listener function fails to trigger when a notification is received on the Android Emulator.

Upon inspecting the OneSignal database (OneSignal.db) using Android Studio's App Inspection feature, I observed that it correctly stores all sent messages. However, the foregroundWillDisplay event only sporadically fires within my app.

Although I sought a resolution by referring to the troubleshooting guide at this link, it doesn't address the specific issue I'm facing: the app receives notifications and the package stores them, yet the event listener remains inactive.

A section in the troubleshooting guide mentions a "Foreground Notification Received Event" but the linked documentation at this page does not elaborate on it. This discrepancy raises questions about the documentation's accuracy and currency.

Suspecting a threading problem, I inspected the code despite my limited Java proficiency. By inserting log statements into the onWillDisplay function here, the issue seemingly resolved itself. Remarkably, the addition of these logs consistently rectified the problem. Although I conducted about 10 tests, both successful and unsuccessful outcomes were reproducible.

It's common for logging to resolve threading issues as it alters code execution. Consequently, I adjusted the synchronized(){} block and present the modified function below. With this alteration, the notification handling consistently functions as expected:

@Override
public void onWillDisplay(INotificationWillDisplayEvent event) {

    synchronized (event) {
      
        if (!this.hasAddedNotificationForegroundListener) {
            event.getNotification().display();
        }

        INotification notification = event.getNotification();
        String notificationId = notification.getNotificationId();
        notificationWillDisplayCache.put(notificationId, (INotificationWillDisplayEvent) event);
        event.preventDefault();

        try {

            sendEvent(
                "OneSignal-notificationWillDisplayInForeground",
                RNUtils.convertHashMapToWritableMap(
                    RNUtils.convertNotificationToMap(notification))
            );
              
            while (preventDefaultCache.containsKey(notificationId)) {
                event.wait();
            }
        }

        catch(InterruptedException e){
            Log.e("InterruptedException" + e.toString(), null);
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
      
    }
}

While I lack expertise in Java development and the inner workings of this package, I hope the maintainer can investigate this fix and provide insights on its validity.

Steps to reproduce?

Follow the documentation from the website regarding setting up OneSignal SDK for ReactNative. Add a listener as described above. Then start your emulator and try to log something from your event listeners. They will only be randomly called. Tested with Android Emulator.

What did you expect to happen?

See the main description above.

React Native OneSignal SDK version

5.0.6

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
@samsnori samsnori changed the title [Bug]: Possibly threading issue with willDisplay on Android + documentations clarification [Bug]: Possibly threading issue with willDisplay on Android + documentatios clarification Mar 23, 2024
@samsnori samsnori changed the title [Bug]: Possibly threading issue with willDisplay on Android + documentatios clarification [Bug]: Possibly threading issue with willDisplay on Android + documentation clarification Mar 23, 2024
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

1 participant