Firebase realtime database - read the latest item under multiple child objects - firebase

I'm trying to write the Firebase realtime database ValueEventListener for the following dataset. I've gone thru the documentation but couldn't the figure out way to read the data for the following scenario i.e latest message under every person object. Seems we can use orderByValue() and limitToFirst() to get the latest message under a specific child (person object), but here I'm trying to read the latest messages under multiple child objects.
Dataset:
{
"messages":{
"person-1":{
"shdjkashdjash":
{
"message": "Hi",
"date": 1544061643815
},
"ashakjshasasjhda":
{
"message": "Hello",
"date":1544061724422
}
},
"person-2":{
"ahsgfasgfhagf":
{
"message": "How are you",
"date": 1544061773193
},
"sfjksfsaflashf":
{
"message": "Hey",
"date":1544061777661
}
},
"person-3":{
"fashfahfahf":
{
"message": "This is the sample example",
"date": 1544061784096
},
"sdfsfhshfsjhfshf":
{
"message": "Firebase messages",
"date":1544061789927
}
}
}
}

Related

Create new firestore document with REST API

I'm trying to create a new document id and store my collection inside it. My post looks like this:
{
"name": "projects/<project-id>/databases/(default)/documents/users/cena123",
"fields": {
"name": {
"stringValue": "tom"
},
"lname": {
"stringValue": "cruise"
}
ERROR i get:
error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \" {\r\n \"name\": \"projects/mountain-bear-****/databases/(default)/documents/users/cena123\",\r\n
What T'm trying to achieve:
Create a document called: "cena123"
Add name and lname fields under the document
The reason you are receiving "code": 400, "message": "Invalid JSON payload received. Unknown name error, is because name should not be passed in the request body, unless it's inside the fields object.
Use the below json in your request body.
{
"fields": {
"name": {
"stringValue": "tom"
},
"lname": {
"stringValue": "cruise"
}
}
}

Sending a notification through Firebase's FCM API Test

I'm currently trying to send a notification using the " Try this API " interface of Firebase.
I filled my Request Body with this :
{
"validateOnly": false,
"message": {
"notification": {
"body": "Body",
"title": "Title"
}
}
}
and the RequestParameters with
projects/myprojectid
I didn't check the Google API Key since my project doesn't have one, but I checked the Google OAuth 2.0.
After executing, I get this error :
{
"error": {
"code": 400,
"message": "Recipient of the message is not set.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message",
"description": "Recipient of the message is not set."
}
]
},
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
}
]
}
}
After some research, I don't understand what is missing in the request. Are all the parameters mandatory for the request ?
You're not specifying who the message is for, which is what the error message is trying to tell you. You can do by either including a token key with one of more device tokens, or a topic or condition key in the JSON.
Also see the Firebase documentation on building requests to send a message for more information and examples of each of these.

Google Firestore - REST API without pagination

I've set up a database in Google Firebase - Firestore and already uploaded just about 500 documents into a single collection.
Now, I've also figured out the REST API to read "all" of the documents from that collection:
GET https://firestore.googleapis.com/v1/projects/my-project/databases/(default)/documents/my-collection
This request returns following response structure:
{
"documents": [
{
"name": "projects/my-project/databases/(default)/documents/my-collection/3D-Technologien7a69809a-dc97-4663-a661-2df3f6f034dd",
"fields": {
"start": {
"stringValue": "28.10.2021"
},
"location": {
"stringValue": "Hb"
},
"name": {
"stringValue": "3D-Technologien"
},
"end": {
"stringValue": "30.10.2021"
}
},
"createTime": "2020-10-09T16:22:53.865356Z",
"updateTime": "2020-10-09T16:22:53.865356Z"
}
],
"nextPageToken": "AFTOeJymPSjChtwIgEMwViyHTAnXjWr_wNAQ7t8h9ZioAI-tJRX7WxdS7TBCeHvREsNgmEGOSLtuDI_YCjIWU2BdlT1-QDoIPHtG4QCxxcjJaSB5NRA-o7smLhr6Pxil0tPUlLBdiLwyjOD-c7qm7QNTR9SqL7HNI3uNKbZi7MG3ng-WMLpoN6MWZ7uGjQGpgfysbwk"
}
It's pretty clear that the response is based on pagination, but I don't find any documentation on this and even worse, on how to adjust this. Currently, max. 20 documents are being returned for the request. I want all 500 to be returned.
Is there any configuration option for this?
According to the docs you can use query params pageSize and pageToken
?pageSize=20&pageToken=ABCDEF1234567890
https://cloud.google.com/firestore/docs/reference/rest/v1beta1/projects.databases.documents/list

How should a "write" request be structured for Firestore REST API (v1beta1)?

Based on the Google Discovery document, and RPC reference, it appears that the :write resource should be available for Firestore database interactions, but performing such a request to my (POST https://firestore.googleapis.com/v1beta1/projects/[my project]/databases/(default)/documents:write) results in:
[
{
"error": {
"code": 400,
"message": "Invalid value (Object), ",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid value (Object), "
}
]
}
]
}
}
]
Is this possible? A related SO answer alludes to this being available as a means of field transforms, the same reason I require this, but I cannot construct valid a JSON body to succeed in the request. Currently, variations on the following don't work as expected when trying a minimum successful response:
{
"writes": [
{
"update": {
"name": "projects/{projectId}/databases/[my project]/documents/exampleId",
"fields": {
"example": {
"integerValue": 100
},
"timestamp": {
"nullValue": null
}
},
"transform": {
"document": "projects/[my project]]/databases/(default)/documents/examples/exampleId",
"fieldTransforms": [
{
"fieldPath": "timestamp",
"setToServerValue": "REQUEST_TIME"
}
]
}
}
}
]
}
First of all, note that you should use the v1 version of the REST API, not the betas.
To create a document, you would use the createDocument method, while to update a document you would use the patch one.
For the document creation you should therefore make a POST HTTP Request to the following URL
https://firestore.googleapis.com/v1/projects/<your-project-id>/databases/(default)/documents/<the-desired-collection>
with the following Request body:
{
fields: {
example: {
integerValue: 100
}
}
}
You need to use documents:commit instead of documents:write
also the name field should be in this format:
"name": "projects/projectID/databases/(default)/documents/collectionName/DocumentId"
See this post.

Customize JSON output of Forms/FormErrors

I would like to customize the JSON output of all the SymfonyForms in my Application. Since this is the first time I am building such an Application I am unsure about the best place to put the code to. Right now I see two options:
A custom Handler within the FOSRestBundle as described here
A Handler within the JMSSerializerBundle as mentioned here
I guess that both ways will do the Job, but which one should I prefer here in terms of flexibility and maintainability. As already said above, I would like to change the output for ALL forms and the validation errors in my application. It would be very nice if a little example would be provided.
Right now I get this output if a validation Error occurs:
{
"code": 400,
"message": "Validation Failed",
"errors": {
"children": {
"username": {},
"password": {
"children": {
"first": {},
"second": {}
}
},
"email": {
"errors": ["This value is already used."]
}
}
}
}
And would like to have something like that:
{
"code": …,
"message": …,
"errors": {
"<formname>": {
…
"email": "This value is already used." //errors concatenated to a string
…
}
}
}
Thanks in ahead!

Resources