Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The bug on resume via native dialog on Android. #225

Open
ForJobOk opened this issue Oct 28, 2024 · 0 comments
Open

The bug on resume via native dialog on Android. #225

ForJobOk opened this issue Oct 28, 2024 · 0 comments

Comments

@ForJobOk
Copy link

Describe the bug

Summary

On Android only, once an app is opened after receiving data from a Branch.io link, the callback fires again when the native dialog is opened and closed.

When I manually put the app in the background and come back to the app, this problem does not occur.
I tried the same problem in a minimal configuration as shown in the video, so I suspect it is a BranchSDK bug.

BranchNativeDialogBug.mp4

My code.

using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Web;
using Newtonsoft.Json.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ReceiveDemo : MonoBehaviour
{
    [SerializeField] private TMP_Text _text;
    [SerializeField] private Button _clearButton;
    [SerializeField] private Button _cameraButton;
    [SerializeField] private Button _locationButton;

    private void Start()
    {
        if (Application.isEditor)
        {
            var uri = new Uri("https://hoge.test-app.link/?$desktop_url=https://hoge/?scene=5d755773-74f5-44d6-825b-e08cf218dcbc");
            Parse(uri);
            return;
        }
        Branch.initSession(CallbackWithBranchUniversalObject);
        
        _clearButton.onClick.AddListener(() => _text.text = "");
        _cameraButton.onClick.AddListener(() => UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.Camera));
        _locationButton.onClick.AddListener(() => UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.FineLocation));
    }

    private void CallbackWithBranchUniversalObject(
        BranchUniversalObject buo,
        BranchLinkProperties linkProps,
        string error)
    {
        if (error != null)
        {
            Debug.LogError("Error on CallbackWithBranchUniversalObject: " + error);
        }

        if (buo == null) return;
        Parse(new Uri(GetLink(buo.ToJsonString())));
    }

    private static string GetLink(string jsonString)
    {

        var jsonObject = JObject.Parse(jsonString);
        var metadata = (string)jsonObject["metadata"];
        if (metadata == null) return null;

        var metadataObject = JObject.Parse(metadata);
        var referringLink = (string)metadataObject["~referring_link"];
        if (referringLink == null) return null;

        var match = Regex.Match(referringLink, @"https:\/\/[a-zA-Z0-9.-]*app\.link\/?.*");
        referringLink = match.Success ? match.Value : referringLink;

        var ampersandIndex = referringLink.IndexOf("&", StringComparison.Ordinal);
        if (ampersandIndex != -1)
        {
            referringLink = referringLink.Substring(0, ampersandIndex);
        }

        Debug.Log("Referring Link: " + referringLink);
        return referringLink;
    }

    private void Parse(Uri uri)
    {
        Debug.Log($"Try to parse : {uri}");
        
        var queryStrings = new NameValueCollection();

        if (Regex.IsMatch(uri.DnsSafeHost, "app\\.link"))
        {
            Debug.Log("URL is included app.link");
            queryStrings = HttpUtility.ParseQueryString(new Uri(HttpUtility.ParseQueryString(uri.Query).Get("$desktop_url")).Query);
        }
        else
        {
            Debug.Log("URL is not included app.link");
            queryStrings = HttpUtility.ParseQueryString(uri.Query);
        }

        var query = "";
        for (int i = 0; i < queryStrings.Count; ++i)
        {
            Debug.Log(queryStrings.GetKey(i) + ": " + queryStrings.Get(i));
            if (!string.IsNullOrEmpty(queryStrings.GetKey(i)))
            {
                query += $"Query{i}: {queryStrings.GetKey(i)}={queryStrings.Get(i)}";
            }
        }

        _text.text = query;
    }
}

Testing Instructions

Please build and run the sample project scene on Android with all settings completed, including my code.

Steps to reproduce

  1. Build and run your sample project scene on Android with all settings completed, including my code.

Expected behavior

Once an app is opened after receiving data from a Branch.io link, the callback does not fire again when the native dialog is opened and closed.

SDK Version

2.0.0

Unity Version

2022.3.24f1

Make and Model

GalaxyS22 / Xperia 5 II

OS

Android12 / 13

Additional Information/Context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant