public class DeepLinkListener : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void DeepLinkReceiverIsAlive();
[DllImport("__Internal")]
private static extern string GetDeepLinkURL();
public bool dontDestroyOnLoad = true;
public static DeepLinkListener instanse;
private bool isDeferredAppLinkFetched = false;
void Start()
{
if (instanse != null)
{
Destroy(gameObject);
}
else
{
instanse = this;
DontDestroyOnLoad(gameObject);
#if UNITY_EDITOR
return;
#endif
DeepLinkReceiverIsAlive(); // Let the App Controller know it's ok to call URLOpened now.
}
}
private void Update()
{
if (!isDeferredAppLinkFetched)
{
if (FB.IsInitialized)
{
//https://developers.facebook.com/docs/unity/reference/current/FB.Mobile.FetchDeferredAppLinkData/
FB.Mobile.FetchDeferredAppLinkData(DeepLinkCallback);
isDeferredAppLinkFetched = true;
}
}
}
private void OnApplicationFocus(bool focus)
{
#if UNITY_EDITOR
return;
#endif
if(focus)
{
var lastUrl = GetDeepLinkURL();
if (lastUrl != String.Empty)
{
Logger.Debug("DEEPLINK: " + lastUrl);
}
}
}
public void URLOpened(string url)
{
Logger.Debug("App opened by link:" + url);
SharedProperties.SetDeepLinkOpened(url);
var eventParams = url.Length > 0 ? SKDeepLinkParser.ParseUrl(url) : null;
if (eventParams != null)
{
AppMetrica.Instance.ReportEvent(MetricaHelper.appOpenedByLink, eventParams);
}
else
{
AppMetrica.Instance.ReportEvent(MetricaHelper.appOpenedByLink);
}
}
void DeepLinkCallback(IAppLinkResult result)
{
var url = result.Url;
if (!string.IsNullOrEmpty(url))
{
SharedProperties.SetDeepLinkOpened(url);
var eventParams = url.Length > 0 ? SKDeepLinkParser.ParseUrl(url) : null;
if (eventParams != null)
{
AppMetrica.Instance.ReportEvent(MetricaHelper.appOpenedByDeferredLink, eventParams);
}
else
{
AppMetrica.Instance.ReportEvent(MetricaHelper.appOpenedByDeferredLink);
}
}
}
}