Inline web-page Android intent fallback detection

Web Push is great, however if the user already has an app installed that does Push notifications the developer needs to reasonably be able to stop either the app or the web sites notification. However there is no shared ID between site and app (for obvious reasons).

There are a couple of strategies that we are experimenting with right now. One of strategy is to try and launch an app and if it is not installed use the web experience.

Android Intents to the rescue. You can make an intent URI that will launch an app and if not there navigate to a page of the developer's choice. One such URL is:

intent:#Intent;package=com.kinlan.sup;S.browser_fallback_url=https%3A%2F%2Fpaul.kinlan.me

This is cool and it works well, however it causes a full on page refresh if you are not careful. Luckily document fragment identifiers work well, so you can just add the following into your page (changing all the parameters for your use case)

<a href="intent:#Intent;package=com.kinlan.sup;S.browser_fallback_url=https%3A%2F%2Fpaul.kinlan.me%2Fandroid-intent-fallback-detection%2F%23noapp;end">Try to open app, but fallback</a>

<script>
  window.onhashchange = function() {
    // If this event fires the app is not installed on the user’s device.
    if(window.location.hash == "#noapp") {
      // Check the hash is what we expect it to be   
    }
  }
</script>

Note: this will only work on Android. Now you can try to launch the app and then start your browser-side Web Push registration logic.

Try to open Android app, but fallback

This is just one step of what you need to do to manage Push Notification in your Web App if you have a complimentary native app. We will be documenting them shortly. Personally I think it is all the more reason just to go web-only.... But I think I am in a minority at the moment :)

I lead the Chrome Developer Relations team at Google.

We want people to have the best experience possible on the web without having to install a native app or produce content in a walled garden.

Our team tries to make it easier for developers to build on the web by supporting every Chrome release, creating great content to support developers on web.dev, contributing to MDN, helping to improve browser compatibility, and some of the best developer tools like Lighthouse, Workbox, Squoosh to name just a few.

I love to learn about what you are building, and how I can help with Chrome or Web development in general, so if you want to chat with me directly, please feel free to book a consultation.

I'm trialing a newsletter, you can subscribe below (thank you!)