Firebase功能日志为空

时间:2018-08-13 23:20:58

标签: javascript firebase google-cloud-functions

因此,当将值添加到数据库中的通知时,我试图从firebase运行以下功能。但是,即使在路径中写入新值,我也不认为该函数被触发运行。该功能的日志保持空白。

由于我无法在此处添加图片,因此我可以尽其所能描述它。在数据库中,我们有通知。在通知中,当某人尝试添加朋友时,它会使用将接收请求的那个人的user_id填充它。然后在该用户user_id中添加一个随机生成的notification_id,在该部分中它既包含发送请求的用户,又包含通知的类型。下面是一个小图

  • 通知
  • ---> user_id
  • ------> notification_id
  • --------->来自
  • --------->类型

index.js

'use strict'

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

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


exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite(event => {


	const user_id = event.params.user_id;
  	const notification_id = event.params.notification_id;

  	console.log('We have a notification to send to : ', user_id);
  
	if(!event.data.val()){
    
    	return console.log('A notification has been deleted from the database : ', notification_id);
  
	}
  
  	const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
  
  	return deviceToken.then(result => {
      
      	const token_id = result.val();
        
      	const payload = {
            notification: {
                title : "Friend Request",
                body : "You-ve recieved a new Friend Request",
                icon : "default",
            }
        };

        return admin.messaging().sendToDevice(token_id, payload).then(response => {

            console.log('This was the notification feature');

        });
    
    });

});

.runtimeconfig.json

{
  "firebase": {
    "projectId": "lapitchat-c1aab",
    "databaseURL": "https://lapitchat-c1aab.firebaseio.com",
    "storageBucket": "lapitchat-c1aab.appspot.com",
    "cloudResourceLocation": "us-central"
  }
}

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.13.0",
    "firebase-functions": "^2.0.0"
  },
  "private": true
}

我是否缺少某些内容或代码中的任何错误?

1 个答案:

答案 0 :(得分:0)

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

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


// There is a new signature for onWrite
exports.sendNotification =
  functions.database.ref('/Notifications/{user_id}/{notification_id}')
    .onWrite((change, context) => {

      const user_id = context.params.user_id;
      const notification_id = context.params.notification_id;

      // Exit when the data is deleted.
      if (!change.after.exists()) {
        console.log('A notification has been deleted from the database : ', notification_id);
        return null;
      }

      console.log('We have a notification to send to : ', user_id);

      return admin.database().ref(`/Users/${user_id}/device_token`).once('value')
        .then(result => {

          const token_id = result.val();

          const payload = {
            notification: {
              title: "Friend Request",
              body: "You-ve recieved a new Friend Request",
              icon: "default"
            }
          };

          return admin.messaging().sendToDevice(token_id, payload);
        })
        .then(response => {
          console.log('This was the notification feature');
          return null;
        })
        .catch(err => {
          console.log(err);
        });
    });

需要启用FCM消息发送API:

https://console.developers.google.com/apis

搜索FCM并在您的项目中启用