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.
Implementation path. Install the Firebase CLI and initialize Cloud Functions in your Firebase project. Write JavaScript code (or TypeScript code to transpile at deployment) to handle events from Firebase services, Google Cloud services, or other event providers. Use the local emulator to test your functions.
You can connect an HTTP function to Firebase Hosting. Requests on your Firebase Hosting site can be proxied to specific HTTP functions. This also allows you to use your own custom domain with an HTTP function.
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/')
Recently the Firebase Team released the "Dynamic Links" feature, that does exactly what you want, and more.
Firebase Dynamic Links
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With