Last active
July 15, 2022 05:44
-
-
Save davidmfinol/d1e9847f21158553c3d8b1f6fabe5826 to your computer and use it in GitHub Desktop.
Unity Firebase Dynamic Links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Start() | |
{ | |
#if !UNITY_WEBGL | |
Debug.Log("CardGameManager::Start:CheckDeepLinks"); | |
CheckDeepLinks(); | |
#endif | |
} | |
private void CheckDeepLinks() | |
{ | |
Debug.Log("Checking Deep Links…"); | |
#if UNITY_IOS | |
Debug.Log("Should use Firebase Dynamic Links for iOS…"); | |
FirebaseApp | |
.CheckAndFixDependenciesAsync() | |
.ContinueWithOnMainThread(task => | |
{ | |
var dependencyStatus = task.Result; | |
if (dependencyStatus != DependencyStatus.Available) | |
{ | |
Debug | |
.LogError("Error with Links! Could not resolve all Firebase dependencies: " + | |
dependencyStatus); | |
Messenger | |
.Show("Error with Links! Could not resolve all Firebase dependencies: " + | |
dependencyStatus); | |
return; | |
} | |
DynamicLinks.DynamicLinkReceived += OnDynamicLinkReceived; | |
Debug.Log("Using Firebase Dynamic Links for iOS!"); | |
}); | |
return; | |
#elif UNITY_ANDROID | |
if (string.IsNullOrEmpty(Application.absoluteURL)) | |
{ | |
DynamicLinks.DynamicLinkReceived += OnDynamicLinkReceived; | |
Debug.Log("Using Firebase Dynamic Links for Android!"); | |
} | |
#else | |
Application.deepLinkActivated += OnDeepLinkActivated; | |
Debug.Log("Using Native Deep Links!"); | |
#endif | |
if (string.IsNullOrEmpty(Application.absoluteURL)) | |
{ | |
Debug.Log("No Start Deep Link"); | |
return; | |
} | |
if ( | |
!Uri | |
.IsWellFormedUriString(Application.absoluteURL, | |
UriKind.RelativeOrAbsolute) | |
) | |
{ | |
Debug | |
.LogWarning("Start Deep Link malformed: " + | |
Application.absoluteURL); | |
return; | |
} | |
Debug.Log("Start Deep Link: " + Application.absoluteURL); | |
OnDeepLinkActivated(Application.absoluteURL); | |
} | |
#if UNITY_ANDROID || UNITY_IOS | |
private void OnDynamicLinkReceived(object sender, EventArgs args) | |
{ | |
Debug.Log("OnDynamicLinkReceived!"); | |
var dynamicLinkEventArgs = args as ReceivedDynamicLinkEventArgs; | |
var deepLink = | |
dynamicLinkEventArgs?.ReceivedDynamicLink.Url.OriginalString; | |
if (string.IsNullOrEmpty(deepLink)) | |
{ | |
Debug.LogError("OnDynamicLinkReceived::deepLinkEmpty"); | |
Messenger.Show("OnDynamicLinkReceived::deepLinkEmpty"); | |
} | |
else | |
OnDeepLinkActivated(deepLink); | |
} | |
#endif | |
private void OnDeepLinkActivated(string deepLink) | |
{ | |
Debug.Log("OnDeepLinkActivated!"); | |
var autoUpdateUrl = GetAutoUpdateUrl(deepLink); | |
if ( | |
string.IsNullOrEmpty(autoUpdateUrl) || | |
!Uri | |
.IsWellFormedUriString(autoUpdateUrl, | |
UriKind.RelativeOrAbsolute) | |
) | |
{ | |
Debug | |
.LogError("OnDeepLinkActivated::autoUpdateUrlMalformed: " + | |
deepLink); | |
Messenger | |
.Show("OnDeepLinkActivated::autoUpdateUrlMalformed: " + | |
deepLink); | |
} | |
else | |
StartCoroutine(GetCardGame(autoUpdateUrl)); | |
} | |
private static string GetAutoUpdateUrl(string deepLink) | |
{ | |
Debug.Log("GetAutoUpdateUrl::deepLink: " + deepLink); | |
if ( | |
string.IsNullOrEmpty(deepLink) || | |
!Uri.IsWellFormedUriString(deepLink, UriKind.RelativeOrAbsolute) | |
) | |
{ | |
Debug | |
.LogWarning("GetAutoUpdateUrl::deepLinkMalformed: " + deepLink); | |
return null; | |
} | |
if (deepLink.StartsWith(Tags.DynamicLinkUriDomain)) | |
{ | |
var dynamicLinkUri = new Uri(deepLink); | |
deepLink = | |
HttpUtility | |
.UrlDecode(HttpUtility | |
.ParseQueryString(dynamicLinkUri.Query) | |
.Get("link")); | |
Debug.Log("GetAutoUpdateUrl::dynamicLink: " + deepLink); | |
if ( | |
string.IsNullOrEmpty(deepLink) || | |
!Uri.IsWellFormedUriString(deepLink, UriKind.RelativeOrAbsolute) | |
) | |
{ | |
Debug | |
.LogWarning("GetAutoUpdateUrl::dynamicLinkMalformed: " + | |
deepLink); | |
return null; | |
} | |
} | |
var deepLinkDecoded = HttpUtility.UrlDecode(deepLink); | |
Debug.Log("GetAutoUpdateUrl::deepLinkDecoded: " + deepLinkDecoded); | |
var deepLinkUriQuery = new Uri(deepLinkDecoded).Query; | |
Debug.Log("GetAutoUpdateUrl::deepLinkUriQuery: " + deepLinkUriQuery); | |
var autoUpdateUrl = | |
HttpUtility.ParseQueryString(deepLinkUriQuery).Get("url"); | |
Debug.Log("GetAutoUpdateUrl::autoUpdateUrl: " + autoUpdateUrl); | |
return autoUpdateUrl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment