12

I am trying to create a download endpoint for my mobile app. The app is available on app store and play store. I want to have a single URL that can be used by users to download the app on either an iOS device or an Android device. I was trying to find out if I can create a cloud function for firebase for this purpose. I was thinking of checking the user-agent for the request and based on whether the request came from an iOS device or an Android device or a web browser, redirect the user to an appropriate URL. I have verified that the device information is present in the user-agent header. For e.g.

"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C89 Safari/602.1 AppEngine-Google; (+http://code.google.com/appengine; appid: s~gcf-http-proxy)"

I guess the next things to do was to redirect the user to the app store URL in the above case. I tried to use res.redirect but that doesn't work. That ends up redirecting the user to cloud-function-base-url/redirect-url instead of just redirect-url.

Below is the code for the cloud function which is just trying to redirect to www.google.com when the function is triggered from the browser.

exports.appDownload = functions.https.onRequest((req, res) => {
  console.log(stringify(req.headers))
  // This will eventually redirect to a store URL based on the phone in user-agent
  res.redirect('www.google.com');
});

This ends up redirecting the browser to base-url/www.google.com instead of just www.google.com and I get an error message saying Not Found. I wanted to redirect the browser to just www.google.com.

I wanted to check if it is possible to achieve the above using a cloud function for firebase.

Varun Gupta
  • 2,870
  • 6
  • 33
  • 73
  • Please show your code that doesn't work the way you expect. – Doug Stevenson Aug 26 '17 at 04:51
  • Sorry, I was just trying to check if it is possible to redirect the browser to a different URL (the app store url in my case) if I invoke a cloud function from a browser. I have posted the code that I tried. – Varun Gupta Aug 26 '17 at 11:14

2 Answers2

30

The documentation for redirect() suggests that you have to give it a fully qualified URL. Note the scheme "https" is present here (you're just giving "www.google.com", which would be taken as a path relative to the current URL):

res.redirect('https://www.google.com/')
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
4

Recently the Firebase Team released the "Dynamic Links" feature, that does exactly what you want, and more.

Firebase Dynamic Links

Andrii Rudavko
  • 370
  • 1
  • 7