【问题标题】:Firebase cloud functions admin.messaging.send() is not a functionFirebase 云函数 admin.messaging.send() 不是函数
【发布时间】:2020-11-05 01:17:02
【问题描述】:

我在 index.js 中创建了一个函数,每当用户有新的活动提要项时,例如当有人喜欢帖子或发表评论或发送消息时,它都会向用户发送通知。当我运行它时,我在 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)
      });
  }
});

【问题讨论】:

    标签: javascript firebase google-cloud-functions firebase-cloud-messaging firebase-admin


    【解决方案1】:

    您需要使用带括号的admin.messaging()。这是一个函数调用,而不是一个属性,就像您已经在使用 admin.firestore() 一样。

    【讨论】:

    • 感谢您指出这一点。但是它仍然没有显示任何通知
    • 听起来您遇到了一个与您第一次发布的问题无关的问题。我建议进行一些调试并再次发布以描述没有按您期望的方式工作的地方。
    • 我发布了通知实现的其余部分
    • 您应该单独发布新问题。 Stack Overflow 每个帖子只允许一个问题或问题。我已将此处的 this 帖子恢复为原来的问题,该问题已经有了答案。在您应用我描述的修复程序后,您的 新 帖子应详细说明现在无法按您预期的方式工作的原因。
    猜你喜欢
    • 2017-10-07
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2017-11-15
    • 2019-01-18
    相关资源
    最近更新 更多