OneSignal / OneSignal-Unity-SDK

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

[Bug]: Get a thread exception in OneSignal Callback methods #543

Closed Arikaton closed 4 days ago

Arikaton commented 2 years ago

What happened?

In callback method i use UnityEngine.Application.version which requires to be called on main thread. I suggest to dispatch onesignal callbacks in unity to main thread. Or update documentation for transparency. In OneSignal versions 2.* java code used unitySendMessage which guarantee that callback will be invoked in main thread.

In fresher versions OneSignal introduced a UnityMainThreadDispatch which only using for OneSiganl.Default.GetTags() calls. I suggest to wrap OneSignal.Default.NotificationOpened, OneSignal.Default.NotificationWillShow, OneSignal.Default.NotificationPermissionChanged in UnityMainThreadDispatch.Post(). Also i submitted a bug with UnityMainThreadDispatch, see #542

Steps to reproduce?

1. Install OneSignal 3.0.4
2. Use any main thread dependent api in OneSignal callback methods
3. Launch Android app
4. Send a notification from OneSignal console
5. Got an exception in OneSignal.Default.NotificationWillShow callback

What did you expect to happen?

The callbacks calls not in main thread in unity

Unity version

2020.3.32

OneSignal Unity SDK version

3.0.4

Platform

Android

Relevant log output

2022-10-17 12:56:15.517 19938-20182/com.game.game E/Unity: UnityException: get_version can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
      at AnalyticsTracker+<LogEventImplementation>d__10.MoveNext () [0x00000] in <00000000000000000000000000000000>:0

Code of Conduct

shepherd-l commented 2 years ago

Thanks for pointing out the issue with callbacks not being dispatched to the main thread. We plan to address this in the next major release

shepherd-l commented 4 days ago

Apologies that this issue has gone stale. In v5 of our Unity SDK, we have changed it so that callbacks are invoked on the main Unity thread. I haven’t seen any further reports of this. Please try upgrading the OneSignal SDK and if this is still an issue, please open a new report with updated information.