我按照这个文档设置FCM,但不起作用。
firebase函数日志中的错误:
ReferenceError:getMessaging未定义
https://firebase.google.com/docs/cloud-messaging/send-message#send-发送到特定设备的消息
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp({ credential: admin.credential.applicationDefault() });
exports.updateUser = functions
.region("australia-southeast1")
.firestore.document("messages/{groupId1}/{groupId2}/{message}")
.onCreate(async (snap, context) => {
console.log("----------------start function--------------------");
const doc = snap.data();
const idFrom = doc.idFrom;
const idTo = doc.idTo;
const contentMessage = doc.content;
admin
.firestore()
.collection("users")
.where("id", "==", idTo)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((userTo) => {
const message = {
message: {
xxxxxxx
},
};
admin
.getMessaging() // <-------
.send(message) // <-------
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
});
});
return { success: true };
});
尝试多次
要使用新的HTTP v1 API在Firebase函数中使用FCM,请使用:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// to get the ADC automatically
admin.initializeApp({ credential: admin.credential.applicationDefault() });
....
admin
.messaging()
.send(message)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
admin.messaging().send(message)
是我认为的老方法,因为在线文档没有提到它。
但它在v1负载下运行良好,iphone的床位数显示出来。