Javascript Firebase云函数admin.messaging.send()不是函数
我在index.js中创建了一个函数,每当用户有一个新的活动提要项时,比如有人喜欢帖子、留言或发送消息时,它都会向用户发送通知。当我运行它时,我在firebase上的函数日志中发现了以下错误:Javascript Firebase云函数admin.messaging.send()不是函数,javascript,firebase,google-cloud-functions,firebase-cloud-messaging,firebase-admin,Javascript,Firebase,Google Cloud Functions,Firebase Cloud Messaging,Firebase Admin,我在index.js中创建了一个函数,每当用户有一个新的活动提要项时,比如有人喜欢帖子、留言或发送消息时,它都会向用户发送通知。当我运行它时,我在firebase上的函数日志中发现了以下错误: TypeError: admin.messaging.send is not a function at sendNotification (/workspace/index.js:220:8) at exports.onCreateActivityFeedItem.functions.f
TypeError: admin.messaging.send is not a function
at sendNotification (/workspace/index.js:220:8)
at exports.onCreateActivityFeedItem.functions.firestore.document.onCreate (/workspace/index.js:176:5)
at process._tickCallback (internal/process/next_tick.js:68:7)
我已经尝试了npm安装firebase-admin@latest
但它没有解决问题。这是我在index.js中创建的函数
exports.onCreateActivityFeedItem = functions.firestore
.document('/feed/{userId}/feedItems/{activityFeedItem}')
.onCreate(async (snapshot, context) => {
console.log('Activity feed item created', snapshot.data());
// 1) Get user connected to the feed
const userId = context.params.userId;
const userRef = admin.firestore().doc(`users/${userId}`);
const doc = await userRef.get();
// 2) Once we have user, check if they have a notification token;
//send notification if they have token
const androidNotificationToken = doc.data().androidNotificationToken;
const createdActivityFeedItem = snapshot.data();
if (androidNotificationToken) {
sendNotification(androidNotificationToken, createdActivityFeedItem);
} else {
console.log('No token for user, cannot send notification');
}
function sendNotification(androidNotificationToken, activityFeedItem){
let body;
// 3) switch body based on activity feed item type
switch (activityFeedItem.type) {
case 'comment':
body = `${activityFeedItem.username} replied:
${activityFeedItem.commentData}`;
break;
case 'like':
body = `${activityFeedItem.username} liked your post`;
break;
case 'follow':
body = `${activityFeedItem.username} started following you`;
break;
case 'request':
body = `${activityFeedItem.username} needs a tutorial in ${activityFeedItem.postId}`;
break;
case 'accepted':
body = `${activityFeedItem.username} accepted your
${activityFeedItem.postId} request`;
break;
case 'message':
body = `${activityFeedItem.username} sent you a message`;
break;
default:
break;
}
// 4) Create message for push notification
const message = {
notification: {body},
token: androidNotificationToken,
data: {recipient: userId},
}
// 5) Send message with admin.messaging()
admin
.messaging
.send(message)
.then(response => {
// response is a message ID string
console.log('Succesfully sent message', response)
})
.catch(error => {
console.log('Error sending message', error)
});
}
});
您需要使用带括号的
admin.messaging()
。这是一个函数调用,而不是属性,就像您已经在使用admin.firestore()
一样,谢谢您指出这一点。但是,它仍然没有显示任何通知。这表明您有一个不同的问题,与您第一次发布的问题无关。我建议进行一些调试,然后再次发布,以描述哪些地方没有按照您期望的方式工作。我发布了通知实现的其余部分。您应该单独发布新问题。堆栈溢出只允许每篇文章有一个问题。我已经把这篇文章回复到原来的问题,这个问题已经有了答案。在你应用了我描述的修复程序之后,你的新帖子应该详细解释什么东西现在没有按照你期望的方式工作。