Android Browser Facebook Redirect Does Not Always Trigger Intent for URL

前端 未结 3 2307
抹茶落季
抹茶落季 2020-12-10 07:30

I\'m in a real pickle and desperately need some help with a critical problem I\'m having.

I\'ve spent months writing a HTML5 app/website along with a native Androi

相关标签:
3条回答
  • 2020-12-10 07:42

    I made some further developments in my search and found that performing a "fake click" was the solution that worked in the majority of scenarios (but not all).

    If the user wishes to share URL http://myapp.com/sharedpage, then I actually post the following URL to Facebook http://myapp.com/share.htm?redirectUrl=sharedpage

    share.htm is just a javascript redirect page that immediately redirects to the appropriate page. It is clever though as on Android instead of just using window.location.replace it uses a fake click of a button with the link which can force an intent to be triggered on some devices/some browsers. Code looks like below.

    <!DOCTYPE HTML>
    <html>
    <body>
        <script type="text/javascript">
            var redirectUrlRelativeToThisPage = ""; // Read off the querystring here
            var isAndroidDevice = (/android/gi).test(navigator.userAgent);
    
            if (isAndroidDevice) {
                // Android device. If the user isn't using a stock browser then window.location.redirect() doesn't always
                // trigger an Intent (and prompt to open Native app) so instead attempt to fake click a hyperlink with the
                // URL as this works more reliably (but not always).
                var linkToFakeClick = document.createElement("a");
                linkToFakeClick.href = redirectUrlRelativeToThisPage;
                var fakeMouseClickEvent = document.createEvent("MouseEvents");
                fakeMouseClickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                linkToFakeClick.dispatchEvent(fakeMouseClickEvent);
            }
            // If we got here either we're not on an Android device or the fake click didn't work so just redirect normally
            window.location.replace(redirectUrlRelativeToThisPage);
        </script>
    </body>
    </html>
    

    In addition to the example code above and if it suited your scenario, you could also incorporate (in the event the fake click didn't work) two different "I do/don't have the app installed" buttons which link to the custom scheme URL (to force an intent) and the usual URL.

    0 讨论(0)
  • 2020-12-10 07:59

    Have you considered to use Facebook deeplinking ? http://developers.facebook.com/docs/mobile/android/deep_linking/

    0 讨论(0)
  • 2020-12-10 08:04

    A very dirty workaround would be to put an intent filter on http://m.facebook.com/l.php:

    <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:scheme="http" 
            android:host="m.facebook.com" 
            android:path="/l.php" />
    </intent-filter>
    

    Then parse the query string and relay the actual URL through another intent if it was not meant for your app.

    Wouldn't recommend it though, since it might look a bit odd, seeing your app as a choice for a link not at all related to your app. Also, it will cause users to be prompted to select an app twice if they've chosen your app the first time. The only time this would work nicely (in the case where the link is not meant for your app) is if phone has your app set as default for all http://m.facebook.com/l.php requests. Still you would be messing with the inner workings of facebook.

    Hopefully android browsers will work more with implicit intents in the future, here is a start: https://code.google.com/p/chromium/issues/detail?id=235060

    0 讨论(0)
提交回复
热议问题