'How to capture custom parameters in firebase deep link in Unity?

everyone!

I'm working for the first time ever in a Unity game project using Firebase.

We configure our deep link in Firebase console to open our game in a specific mode when te deep link was sent.

That part works well. But we have a big issue:

We need to get a value form a custom parameter in the URL, because our server generates a random username for our game to make a Leader Board sandbox everytime the deep link was sent.

So we configure our deep link in Firebase console like this:

https://exemple.page.link/gamemode?username=123

But, in this case, the username will always be 123. However, the server will always send the link with a random user, for example:

https://exemple.page.link/gamemode?username=8b9d-1c6b-c52a3-b0d7

If we leave the username parameter blank, even if the server sent the random username, we receive nothing in our link in Unity, just:

https://exemple.page.link/gamemode?username

If I manage to get the link in a browser in my Android device, I get the random username properly. But when the game opens, this value was lost!

To receive the dynamic link, we just use the void OnDynamicLink(object sender, EventArgs args) in our Firebase Manager script in Unity.

So, my question is:

There is a way to receive the username parameter with a dynamic value? If the answer is 'yes', there's a method to get that custom value? Or I just missed up something in the firebase configuration or even the deep link in Firebase console?

Thanks in advance!



Solution 1:[1]

From Receive Dynamic Links with Unity you need to cast to ReceivedDynamicLinkEventArgs

void OnDynamicLink(object sender, EventArgs args) 
{
    var dynamicLinkEventArgs = args as ReceivedDynamicLinkEventArgs;
    Debug.Log($"Received dynamic link {dynamicLinkEventArgs.ReceivedDynamicLink.Url.OriginalString}");
}

and then if there is only this one parameter anyway you could probably simply do e.g.

var query = dynamicLinkEventArgs.ReceivedDynamicLink.Url.Query;
var username = query.Split('=')[1];

or if there can be more parameter

var query = dynamicLinkEventArgs.ReceivedDynamicLink.Url.Query;
// first Split into the individual patamters
var patamters = query.Split('&');
string username = null;
foreach(var parameter in parameters)
{
    // Then split into key and value
    var keyValue = parameter.Split('=');

    if(keyValue[0] == "username")
    {
        username = keyValue[1];
        break;
    }
}

or if you happen to be a fan of Linq (not sure if this is the most elegant though)

var query = dynamicLinkEventArgs.ReceivedDynamicLink.Url.Query;
var username = query.Split('&').Select(parameter => parameter.Split('=').Where(keyValue => keyValue[0] == "username").Select(keyValue => keyValue[1]).FirstOrDefault();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1