Вы можете добиться этого, просто нажав кнопку ADD COLLECTION
, которая выглядит следующим образом:

Появится новое всплывающее окно, который выглядит следующим образом:

Называйте свою коллекцию любым удобным для вас способом.Я назвал это Products
.Нажмите NEXT
, и снова появится новое всплывающее окно:

Оставьте поле Document id
пустым.Выберите поле (я назвал id productName) и значение типа String с именем Milk
.
Это результат:

Вы можете использовать Облачные функции для Firebase , поэтому каждый раз, когда новая запись (документ) добавляется в определенное место в вашей базе данных Cloud Firebase, вы сможете запускать другую функцию.Для этого я приведу пример.Давайте предположим, что мы хотим отправить уведомление пользователю, когда что-то происходит.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.yourFunction = functions.firestore.document("notifications/{uid}/userNotifications/{notificationId}").onWrite(event => {
const uid = event.params.uid;
const notificationId = event.params.notificationId;
return admin.firestore().collection("notifications").doc(uid).collection("userNotifications").doc(notificationId).get().then(queryResult => {
const uid = queryResult.data().senderUid;
const notificationMessage = queryResult.data().notificationMessage;
const fromUser = admin.firestore().collection("users").doc(senderUid).get();
const toUser = admin.firestore().collection("users").doc(uid).get();
return Promise.all([fromUser, toUser]).then(result => {
const fromUserName = result[0].data().userName;
const toUserName = result[1].data().userName;
const tokenId = result[1].data().tokenId;
const notificationContent = {
notification: {
title: "Your Title",
body: notificationMessage,
icon: "default",
sound : "default"
}
};
return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
console.log("Notification sent!");
});
});
});
});
Поэтому, когда новая операция записи выполняется по этому пути notifications/{uid}/userNotifications/{notificationId}
, другими словами, появляется новый документ, который мы можем создать.Promise
и отправьте уведомление.