【问题标题】:Cloud function for firebase to redirect to a URL用于 Firebase 重定向到 URL 的云功能
【发布时间】:2017-08-26 00:37:12
【问题描述】:

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

"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的云功能来实现上述目标。

【问题讨论】:

  • 请显示您的代码不能按您预期的方式工作。
  • 抱歉,如果我从浏览器调用云功能,我只是想检查是否可以将浏览器重定向到不同的 URL(在我的例子中是应用商店 URL)。我已经发布了我尝试过的代码。

标签: firebase google-cloud-functions


【解决方案1】:

documentation for redirect() 建议您必须为其提供完全限定的 URL。请注意,此处存在“https”方案(您只是给出“www.google.com”,它将被视为相对于当前 URL 的路径):

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

【讨论】:

  • 太棒了,感谢您的回答。有用。去火力基地!
【解决方案2】:

最近 Firebase 团队发布了“动态链接”功能,这正是您想要的,等等。

Firebase Dynamic Links

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 2019-08-05
    • 2020-10-14
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多