【问题标题】:Firebase Function Logs emptyFirebase 函数日志为空
【发布时间】:2019-01-20 17:48:47
【问题描述】:

因此,当向我的数据库中的通知添加值时,我试图从 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
}

我的代码中是否有遗漏或任何错误?

【问题讨论】:

    标签: javascript firebase google-cloud-functions


    【解决方案1】:
    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 并在您的项目中启用

    【讨论】:

    • 感谢您的回复。我已经尝试了您发送的代码并启用了 FCM,但是仍然没有日志。我可以在数据库中看到如何写入新通知。
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多