OneSignal-Unity-SDK icon indicating copy to clipboard operation
OneSignal-Unity-SDK copied to clipboard

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

Open Arikaton opened this issue 3 years ago • 1 comments

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

  • [X] I agree to follow this project's Code of Conduct

Arikaton avatar Oct 17 '22 12:10 Arikaton

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 avatar Nov 04 '22 15:11 shepherd-l