I pushed the following FCM push notification to firebase functions:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
exports.newSubscriberNotification = functions.firestore
.document('messages/{id}')
.onUpdate(async event => {
const data = event.after.data();
const content = data ? data.content : '';
const toUserId = data ? data.toUserId : '';
const payload = {
notification: {
title: 'New message',
body: `${content}`
}
};
const db = admin.firestore();
const devicesRef = db.collection('devices').where('userId', '==', toUserId);
const devices = await devicesRef.get();
const tokens: any = [];
devices.forEach(result => {
const token = result.data().token;
tokens.push(token);
});
return admin.messaging().sendToDevice(tokens, payload);
});
In the code I'm using .onUpdate, which I assume should trigger when one of the messages collection is updated. This is a messaging app so when ever the messaging collection is updated I'd like to trigger a push notification to the receiving user.
I get the tuUserId and use it to get the device token of that user from the devices collection. I'm testing now so the toUserId is identical to the from userId because it's just using my device...so hopefully when I update the message.doc() it sends a push notification.
Here is the message collection in firebase:
This is the function error log:
11:04:22.937 PM
newSubscriberNotification
Function execution started
11:04:22.946 PM
newSubscriberNotification
Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value.
at Object.validateUserInput (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:273:15)
at validateQueryValue (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1844:18)
at CollectionReference.where (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:956:9)
at exports.newSubscriberNotification.functions.firestore.document.onUpdate (/srv/lib/index.js:19:49)
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
at /worker/worker.js:825:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
11:04:22.957 PM
newSubscriberNotification
Function execution took 21 ms, finished with status: 'error'
I'm pretty sure it doesn't like my .where clause because of this error: at CollectionReference.where (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:956:9)
I'm just not sure why