用于 firebase 的云函数重定向到 URL

Cloud function for firebase to redirect to a URL

我正在尝试为我的移动应用程序创建下载端点。该应用程序可在应用程序商店和 Play 商店中找到。我想要一个 URL 可供用户用来在 iOS 设备或​​ Android 设备上下载应用程序。我试图找出是否可以为此目的为 firebase 创建一个云函数。我正在考虑检查请求的 user-agent,并根据请求是来自 iOS 设备还是 Android 设备或​​ Web 浏览器,将用户重定向到适当的 URL。我已验证 user-agent header 中存在设备信息。例如

"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)"

我想接下来要做的是在上述情况下将用户重定向到应用程序商店 URL。我尝试使用 res.redirect 但这不起作用。最终将用户重定向到 cloud-function-base-url/redirect-url 而不是 redirect-url.

下面是云函数的代码,当从浏览器触发该函数时,它只是试图重定向到 www.google.com。

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');
});

这最终将浏览器重定向到 base-url/www.google.com 而不是 www.google.com,我收到一条错误消息 Not Found。我想将浏览器重定向到 www.google.com.

我想看看是否可以使用 firebase 的云函数来实现上述目标。

documentation for redirect()建议你必须给它一个完全合格的URL。请注意此处存在方案 "https"(您只是给出 "www.google.com",这将被视为相对于当前 URL 的路径):

res.redirect('https://www.google.com/')

最近 Firebase 团队发布了 "Dynamic Links" 功能,它完全可以满足您的需求,甚至更多。

Firebase Dynamic Links