推送通知会返回Google Cloud Functions

时间:2017-09-28 21:28:09

标签: node.js firebase firebase-cloud-messaging google-cloud-functions econnreset

我在Firebase云功能中有一项功能,它会检索用户的设备组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
  });
}

0 个答案:

没有答案