Firebase云功能/通知

时间:2018-06-06 19:33:44

标签: firebase firebase-cloud-messaging google-cloud-firestore google-cloud-functions

我正在尝试使用firebase的云功能进行部署通知功能 我做了代码的第一部分,并成功完成了:

'use-strict'
 const functions = require('firebase-functions');

 //const paypal=require('paypal-rest-sdk');

 const admin=require('firebase-admin');


 admin.initializeApp(functions.config().firebase);

 exports.sendNotification=functions.firestore

 .document("Users/{user_id}/Notifications/{notification_id}")
 onWrite((change, context)=>{

  const user_id=context.params.user_id;

  const notification_id=context.params.notification_id;
       });

没有回复承诺,这是好的,但当我试图这样完成时:

'use-strict'
 const functions = require('firebase-functions');
  //const paypal=require('paypal-rest-sdk');
   const admin=require('firebase-admin');
   admin.initializeApp(functions.config().firebase);
    exports.sendNotification=functions.firestore
     .document("Users/{user_id}/Notifications/{notification_id}")
      .onWrite((change, context)=>{
     const user_id=context.params.user_id;
     const notification_id=context.params.notification_id;
    return admin.firestore().collection("Users").doc(user_id)
    .collection("Notifications")
    .doc(notification_id)
     .get()
       .then(queryResult=>{
          const from_user_id=queryResult.data().from;
         const from_data=admin.firestore()
                     .collection("Users").doc(from_user_id).get();
          const to_data=admin.firestore().collection("Users")
                 .doc(user_id).get();
           return Promise.all([from_data,to_data]).then(result=>{
            const from_name=result[0].data().name;
             const to_name=result[1].data().name;
             console.log("from :"+from_name+"TO"+to_name);

                    });
                });

// console.log("user_id"+ user_id+ "notification_id"+notification_id);
                 });

我在node.cm中有这两个错误: 1 - 避免嵌套承诺 2-each then()应该返回一个值 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

当Firestore数据库发生更改时,通过FCM和云功能发送推送通知

您可以尝试这样

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.firestore
       .document('/users/{documentId}')
       .onWrite((change, context) => {

console.log("CHANGE in DOCUMENT ID : " + context.params.documentId);

// Fetch data using standard accessors
const userId = change.after.data().userId;
const ownerId = change.after.data().ownerId;

console.log('We have a new request UID:', userId, 'for owner:', ownerId);


const owner_data = admin.firestore().collection('users').doc(ownerId).get();
const user_data = admin.firestore().collection('users').doc(userId).get();

return Promise.all([owner_data, user_data]).then(result => {

const ownerFCMToken = result[0].data().fcmToken;
const userName = result[1].data().displayName;

console.log("OWNER Token : " + ownerFCMToken + " USER: " + userName);

const payload = {
        notification: {
          title: 'Test title!',
          body: `${userName} sent you a following request.`
          // icon: follower.photoURL
        }
      };

admin.messaging().sendToDevice(followedFCMToken, payload)
    .then(function (response) {
        console.log("Push response : " + response);
        return response
    })
    .catch(function (error) {
        console.error("Error in sending push");
    });
});