-
-
Notifications
You must be signed in to change notification settings - Fork 60
Description
Problem
Currently, when users put the app in the background during startup, the SDK has no way to compensate for the inactivity, still finishing the transaction based on timestamps. Which can result in reporting unrealistically long startups (i.e. 4hrs).
Context
The SDK relies on MonoBehaviour's built-in functionality to get notified about the app's state OnApplicationPause and OnApplicationFocus here:
sentry-unity/src/Sentry.Unity/SentryMonoBehaviour.cs
Lines 88 to 97 in 0558392
| /// <summary> | |
| /// To receive Pause events. | |
| /// </summary> | |
| internal void OnApplicationPause(bool pauseStatus) => UpdatePauseStatus(pauseStatus); | |
| /// <summary> | |
| /// To receive Focus events. | |
| /// </summary> | |
| /// <param name="hasFocus"></param> | |
| internal void OnApplicationFocus(bool hasFocus) => UpdatePauseStatus(!hasFocus); |
This creates a gap in the SDK's coverage during startup since the MonoBehaviour has not loaded yet.
Proposal
Instead, the SDK should move that functionality outside of the This does not work. The MonoBehaviour and make use of Application.focusChanged. Since this is supported only on Unity 2021 and newer we'll have to make this conditional.Application.focusChanged is only ever invoked after Awake and before Start.
It looks like we'd need to build our own integration for this.
Android
We can implement our own hook into ActivityLifeCycleCallbacks.
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (var application = activity.Call<AndroidJavaObject>("getApplication"))
{
AndroidJavaProxy lifecycleCallbacks = new AndroidLifecycleCallbacks();
application.Call("registerActivityLifecycleCallbacks", lifecycleCallbacks);
}
iOS
TBD
Metadata
Metadata
Assignees
Labels
Projects
Status