title: Android Browser Facebook Redirect Does Not Always Trigger Intent for URL
body: <p>I'm in a real pickle and desperately need some help with a critical problem I'm having.  </p>
  
  <p>I've spent months writing a HTML5 app/website along with a native Android application that is simply a WebView wrapper for the HTML5 website.  One of the core features of the application is that users can share app specific URLs to Facebook and Twitter etc so that their friends can follow the shared URLs which will open up either the HTML5 version of my app in their browser OR MOST IMPORTANTLY if they are on Android and they have my native Android app installed they are PROMPTED TO OPEN IN MY APP.</p>
  
  <p>This is a rather long complicated issue so to simplify I'll use some certain terms consistently through-out this post:</p>
  
  <ul>
  <li>"MyApp user" a Android device user who DOES have my native Android app installed</li>
  <li>"non-MyApp user" a Android device user who DOES NOT have my native Android app installed</li>
  <li>"stock browser user" a stock Android user who uses a stock browser </li>
  <li>"non-stock browser user" a Android user who uses a non-stock browser (note the Samsung Galaxy SII stock "Internet" app is considered non-stock)</li>
  <li>"choose app dialog" on Android the OS dialog that appears asking the user what application they want to open the action/intent/url/document with and if they want to default to use this application always</li>
  </ul>
  
  <hr>
  
  <p>My AndroidManifest.xml contains the following...</p>
  
  <pre><code>&lt;intent-filter&gt;
    &lt;action android:name="android.intent.action.VIEW" /&gt;
    &lt;category android:name="android.intent.category.DEFAULT" /&gt;
    &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
    &lt;data android:scheme="http" android:host="myapp.com" android:pathPrefix="/" /&gt;
  &lt;/intent-filter&gt;
  </code></pre>
  
  <p>When tested this WORKS PERFECTLY in any normal scenario. For example if a "MyApp user" follows a direct link to <a href="http://myapp.com/sharedpage">http://myapp.com/sharedpage</a>, the "choose app dialog" appears. Great, I've definitely configured my AndroidManifest.xml correctly.</p>
  
  <p>However things do NOT ALWAYS WORK in the critical Facebook sharing scenerio.  It does not always work because both Facebook's native Android app and Facebook's mobile website (http://m.facebook.com) DO NOT LINK DIRECTLY to the shared URLs but instead link via a Facebook redirect page.  For example if <a href="http://myapp.com/sharedpage">http://myapp.com/sharedpage</a> is shared, Facebook will ultimately deliver the following URL:</p>
  
  <p><a href="http://m.facebook.com/l.php?u=http%3A%2F%2Fmyapp.com%2Fsharedpage&amp;h=EAQGpLtuF&amp;enc=AZMXYZg7XwQ39zlWkKSGnLw62lEbtbMeWFmRwRZINoOcg0UgZe3fUVPgqQzV1nuTipSVnquV3a3ovqu7HQFUf3bb3ZJ1gYG8dEOJXzPf6RJGflf9_x8w-6CCXu8G2VZqgfD7lx6EbLTSKLnF56_o5khHybycPUlhpdfLsk6M9muer4jMOmPK6_kfjTq2gvnYNNpStcF0ilJD6nacPqx_1xsdYkUMpKYWbJfSo7qqv1S5xT5KRaLPxl8zmAkYc0FhwyTdn-tUGwRBbbdM4QCd2Z75Tb_VeJG3LvbDwFAbp6G3kH3LOSxVtTd5MST4pUW8xmhNeTUVBVXV16OD27QcsSWOlEfL72fxn11PDE5s4WWsXMnwhDJLUAWOAna7lziBnWzjZdlQK_amI9nhcegaOLDLNFCp125rZS3jxFXf7gtF9g0BsmnPZ2Gjxkc6UgQXhEYldllq9nwpShGbnZDlSg0_&amp;s=1">http://m.facebook.com/l.php?u=http%3A%2F%2Fmyapp.com%2Fsharedpage&amp;h=EAQGpLtuF&amp;enc=AZMXYZg7XwQ39zlWkKSGnLw62lEbtbMeWFmRwRZINoOcg0UgZe3fUVPgqQzV1nuTipSVnquV3a3ovqu7HQFUf3bb3ZJ1gYG8dEOJXzPf6RJGflf9_x8w-6CCXu8G2VZqgfD7lx6EbLTSKLnF56_o5khHybycPUlhpdfLsk6M9muer4jMOmPK6_kfjTq2gvnYNNpStcF0ilJD6nacPqx_1xsdYkUMpKYWbJfSo7qqv1S5xT5KRaLPxl8zmAkYc0FhwyTdn-tUGwRBbbdM4QCd2Z75Tb_VeJG3LvbDwFAbp6G3kH3LOSxVtTd5MST4pUW8xmhNeTUVBVXV16OD27QcsSWOlEfL72fxn11PDE5s4WWsXMnwhDJLUAWOAna7lziBnWzjZdlQK_amI9nhcegaOLDLNFCp125rZS3jxFXf7gtF9g0BsmnPZ2Gjxkc6UgQXhEYldllq9nwpShGbnZDlSg0_&amp;s=1</a></p>
  
  <p>If a "MyApp user" follows one of these Facebook links one of two things might happen depending on the browser they are using...</p>
  
  <p><strong>MyApp Stock Browser User:</strong></p>
  
  <p>If the user has stock Android installed and uses the stock Android browser then things work FINE because the following events occur...</p>
  
  <ul>
  <li>Intent fires for the FB l.php URL</li>
  <li>FB URL gets opened in the ANDROID STOCK BROWSER</li>
  <li>The FB l.php is loaded and initiates the REDIRECT to <a href="http://myapp.com/sharedpage">http://myapp.com/sharedpage</a> </li>
  <li><strong>Intent fires for the <a href="http://myapp.com/sharedpage">http://myapp.com/sharedpage</a> URL</strong></li>
  <li>"choose app dialog" prompts user to open in MY NATIVE APP or in browser</li>
  </ul>
  
  <p><strong>MyApp Non-Stock Browser User:</strong></p>
  
  <p>HOWEVER if the user...</p>
  
  <ol>
  <li>has installed and uses a NON-stock Android browers, e.g. Dolphin HD/mini, Opera Mobile/Mini etc, or</li>
  <li>has a manufacturer customised version of Android (e.g. on Samsung Galaxy SII, etc) and therefore has a customised browser </li>
  </ol>
  
  <p>... then here's the MAJOR ISSUE because the following events occur...</p>
  
  <ul>
  <li>Intent fires for the FB l.php URL </li>
  <li>FB URL gets opened in the NON-STOCK BROWSER</li>
  <li>The FB l.php is loaded and initiates the REDIRECT to <a href="http://myapp.com/sharedpage">http://myapp.com/sharedpage</a> </li>
  <li><strong>Intent IS NOT FIRED by the non-stock browser, the "choose app dialog" DOES NOT appear, the user is NOT prompted to open URL in my app</strong></li>
  <li>The <a href="http://myapp.com/sharedpage">http://myapp.com/sharedpage</a> URL is loaded and rendered in a tab of the non-stock browser</li>
  </ul>
  
  <hr>
  
  <p><strong>Client Side Redirect</strong></p>
  
  <p>In order to confirm its the redirect that was causing the problem,  I created a very simple HTML page named "clientSideRedirector.htm"</p>
  
  <pre><code>&lt;html&gt;&lt;body&gt;&lt;script&gt;
     window.location.href = "http://myapp.com/sharedpage";
  &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>If My-App/non-stock browser user opens <a href="http://myapp.com/clientSideRedirector.htm">http://myapp.com/clientSideRedirector.htm</a> the "app choice dialog" does NOT appear.  FAIL.</p>
  
  <p>If My-App/stock browser user opens <a href="http://myapp.com/clientSideRedirector.htm">http://myapp.com/clientSideRedirector.htm</a> the "app choice dialog" DOES appear.</p>
  
  <p>This would seem to tally up with what we're seeing with the Facebook share/redirect.</p>
  
  <p><strong>Server Side (302) Redirect</strong></p>
  
  <p>I also thought I'd try a server side redirect so I created a .NET dotNetRedirect.ashx page:</p>
  
  <pre><code>public class ShareRedirect : IHttpHandler
  {
      public void ProcessRequest(HttpContext context)
      {
              context.Response.Redirect("http://myapp.com/sharedpage");
      }
      public bool IsReusable { get { return false; } }
  } 
  </code></pre>
  
  <p>This has a very interesting and different result to the client redirect.<br>
  If My-App user opens the <a href="http://myapp.com/dotNetRedirect.htm">http://myapp.com/dotNetRedirect.htm</a> the "app choice dialog" DOES appear REGARDLESS of the users browser.
  So this type of redirect seems to work!</p>
  
  <hr>
  
  <p><strong>Attempted Solution A</strong> </p>
  
  <p>(Double redirect: Facebook l.php redirect to server-side redirect to app URL)</p>
  
  <p>I thought this could be the solution to my Facebook sharing problems.  If I shared <a href="http://myapp.com/dotNetRedirect.ashx">http://myapp.com/dotNetRedirect.ashx</a> URL to Facebook then maybe Facebook would redirect to the dotNetRedirect.htm page and then the server side redirect would force the "choose app dialog" prompt to open. </p>
  
  <p>Unfortunately this DOES NOT work, in a non-stock browser it would appear if the first redirect doesn't trigger the intent/"choose app dialog" further redirects won't either. Massively gutting.</p>
  
  <hr>
  
  <p><strong>Attempted Solution B</strong> </p>
  
  <p>(Using a custom scheme)</p>
  
  <p>Having exhausted the server-side idea I thought I'd investigate using a custom URI scheme to trigger the "app choice dialog".</p>
  
  <p>AndroidManifest.xml:</p>
  
  <pre><code>&lt;intent-filter&gt;
    &lt;action android:name="android.intent.action.VIEW" /&gt;
    &lt;category android:name="android.intent.category.DEFAULT" /&gt;
    &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
    &lt;data android:scheme="myapp" /&gt;
  &lt;/intent-filter&gt;
  </code></pre>
  
  <p>redirect.htm</p>
  
  <pre><code>&lt;html&gt;&lt;body&gt;&lt;script&gt;
     window.location.href = "myapp.com://sharedpage";
  &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>This does actually work, the "choose app dialog" appears via the DB l.php URL even when using a non-stock browser.  However it isn't really a feasible solution as it stands because "non MyApp users" are left with a "Webpage not available" page when redirected to the myapp.com://sharedpage URL. ]</p>
  
  <hr>
  
  <p>Does anyone else have any other bright ideas or suggestions? </p>