【问题标题】:Firebase: Get current time without writing to the database (IOS and ANDROID)Firebase:获取当前时间而不写入数据库(IOS 和 ANDROID)
【发布时间】:2019-01-03 12:26:33
【问题描述】:

我想使用 Firebase 获取服务器上的当前时间,而不是写入数据库然后读取它。

有人知道直接从 Firebase 获取当前时间戳的解决方案吗?

【问题讨论】:

    标签: android ios firebase firebase-realtime-database timestamp


    【解决方案1】:

    是的,有!不管是IOS还是Android,都可以在Cloud Functions for Firebase写一个函数,就这么简单:

    exports.currentTime = functions.https.onRequest((req, res) => {
        res.send({"timestamp":new Date().getTime()})
    })
    

    您可以将其托管在 Cloud Function 中,无需用户交互即可获取服务器时间戳。

    有关如何在 Firebase 实时数据库中设置/读取时间戳的更多信息,请参阅我在此 post 中的回答。

    【讨论】:

    • 感谢您的回答,但如何从客户端获取此值(比如说 android)?
    • 与我在此 post 中解释的方式完全相同,对吧?使用getTimeDate() 方法就可以了。
    • 我的意思是当我部署我的功能时,这个功能如何给我时间?
    • 这个函数是如何触发的,它如何将当前时间返回给我的应用程序?
    • 您需要在 Coud Function 中托管,然后像任何其他功能一样触发。以cron-job.org 为例,对吗?
    【解决方案2】:

    您必须编写一个计算时间的 firebase 云函数。在 firebase 上部署该功能。每当您请求该功能时,它都会为您提供当前时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 2022-01-17
      • 2019-06-15
      • 2018-10-18
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多