firebase / quickstart-cpp

Firebase Quickstart Samples for C++
https://firebase.google.com/games
Apache License 2.0
212 stars 123 forks source link

Dynamic Links on Android with C++ SDK only received if App not started previously #20

Closed ForestRingGames closed 6 years ago

ForestRingGames commented 6 years ago

We've setup dynamic link creation both on iOS and Android with the Firebase Dynamic Link C++ SDK. All is working perfectly fine on iOS, links get received and the Listener is triggered whether the App is closed before or not. This is our Listener code:

void PlatformManager::OnDynamicLinkReceived(const firebase::dynamic_links::DynamicLink *dynamic_link)
{
        MLLOG("[PlatformManager] OnDynamicLinkReceived - Received link: %s", dynamic_link->url.c_str());
}

App Init:

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS

    firebase::AppOptions appOptions;
    _firebaseApp = firebase::App::Create(appOptions);

#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID

    firebase::AppOptions appOptions;
    _firebaseApp = firebase::App::Create(firebase::AppOptions(), JniHelper::getEnv(), JniHelper::getActivity());

#endif

    firebase::dynamic_links::Initialize(*_firebaseApp, this);

Also our manifest setup:

        <activity
            android:name="org.cocos2dx.cpp.AppActivity"
            android:screenOrientation="sensorPortrait"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="towerduel.com" android:scheme="http"/>
                <data android:host="towerduel.com" android:scheme="https"/>
            </intent-filter>
        </activity>

On Android, the log is only triggered when the App was not open previously. If the App was open, then clicking the Link also does correctly open the App, but the listener is not triggered / no log ist printed.

Do you have any idea why this is happening and how we can also make the listener trigger if the app is already open like on iOS?

stewartmiles commented 6 years ago

Unfortunately we can't hook the application lifecycle on Android (on iOS we use method swizzling on the app delegate) so you need to call dynamic_links::Fetch() when the app gains focus to get the link.

https://firebase.google.com/docs/reference/cpp/namespace/firebase/dynamic-links#namespacefirebase_1_1dynamic__links_1ad0a04de4c0b8c875bf6fe39fa90ea420

ForestRingGames commented 6 years ago

Thank you so much, this works!

Really cool support here :) In case you want to add it to the documentation, I followed these guides here: https://firebase.google.com/docs/dynamic-links/android/receive https://firebase.google.com/docs/dynamic-links/cpp/receive

Maybe it'd be cool to add this piece of info to these guides, so people will not make the same mistake in the future.

johnb003 commented 6 years ago

A note was added to the bottom of: https://firebase.google.com/docs/dynamic-links/cpp/receive

"Note: On Android you must also call Fetch() when the application gains focus in order for the listener to be triggered."