2

我在 Firebase Cloud Functions 中有一个函数,它检索用户的设备组 ID 以发送推送通知,然后发送推送通知。如果该函数只被调用一次,这很好用,但是如果我有一组用户我也想发送推送通知,sendPushNotification 函数返回错误:FAILED err= { RequestError: Error: read ECONNRESET at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)每次尝试发送推送

据我了解ECONNRESET,这意味着连接在完成操作之前在一端关闭,可以帮助/解释我为什么会这样:

这是我的代码:

function sendFollowNotification(snapshot) {
  const notificationMsg = getFollowNotificationMsg() //returns a string
  snapshot.forEach(function(singleUser, index) {
  const userId = singleUser.key;
  const userObject = singleUser.val();
  console.log("will get device group")

  if (index + 1 == snapshot.numChildren()) {
    return getDeviceGroupNotificationKey(userId, "Discover new artists", notificationMsg, "", true);
  } else {
    getDeviceGroupNotificationKey(userId, "Discover new artists", notificationMsg, "", false);
  }
}


function getDeviceGroupNotificationKey(groupId, notificationTitle, notificationBody, notificationSubject, shouldReturn) {
    const pathToDeviceGroup = admin.database().ref('deviceGroups').child(groupId);

    pathToDeviceGroup.once("value").then( function(snapshot) {  

      const deviceGroupObj = snapshot.val();
      const notification_key = deviceGroupObj.notification_key;

      console.log("got notification key")
      console.log(notification_key)

      if (notification_key !== undefined) {
          return sendPushToDeviceGroupOld(notification_key, notificationTitle, notificationBody, "notificationKeyOld2", notificationSubject, shouldReturn);
      } else {
          return
      }

    }).catch(reason => {
      console.log("user device group not there")
      return
  })

}


function sendPushToDeviceGroupOld(notification_key, title, body, subject, message, shouldReturn) {
console.log('sending push to ' + notification_key)
const serverKey = '-';
const senderId = '-';

const options = {
  method: 'POST',
  uri: 'https://fcm.googleapis.com/fcm/send',
  headers: {
   'Authorization': 'key=' + serverKey,
   'project_id': senderId
  },
  body: {
   to: notification_key,
   data: {
    subject: message
   },
   notification: {
     title: title,
     body: body,
     badge: 1,
     sound: "default",
    },
   priority : 'high',
   content_available: true
  },
  json: true
};

  return rqstProm(options)
  .then((parsedBody) => {
    console.log('SUCCESS response=', parsedBody);
    return        
  })
  .catch((err) => {
    console.log('FAILED', err);
    return
  });
}
4

0 回答 0