Firebase 云函数 admin.messaging.send() 不是函数
Firebase cloud functions admin.messaging.send() is not a function
我在 index.js 中创建了一个功能,每当用户有新的 activity 提要项目时,就像有人喜欢 post 或发表评论或发送消息。当我 运行 它时,我在 firebase 上的函数日志中收到此错误:
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 install 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()
.
我在 index.js 中创建了一个功能,每当用户有新的 activity 提要项目时,就像有人喜欢 post 或发表评论或发送消息。当我 运行 它时,我在 firebase 上的函数日志中收到此错误:
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 install 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()
.