Firebase: Get current time without writing to the

2020-03-24 08:39发布

问题:

I want to use Firebase to get the current time on server without writing to the database and then reading it.

Any one knows a solution to directly get a current time stamp from Firebase?

回答1:

Yes there is! It doesn't matter if it is for IOS or for Android, you can write a frunction in Cloud Functions for Firebase which will be as easy as:

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

You can host this in Cloud Function and get the server timestamp without user interaction.

For more informations on how to set/read a timestamp in a Firebase Real-time database, please see my answer from this post.



回答2:

You have to write a firebase cloud function that calculates the time. Deploy that function on firebase. Whenever you request that function it will gives you the current time.