Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 我需要在firebase数据库中添加某些内容后发送推送通知我如何才能做到这一点_Node.js_Firebase_Firebase Realtime Database - Fatal编程技术网

Node.js 我需要在firebase数据库中添加某些内容后发送推送通知我如何才能做到这一点

Node.js 我需要在firebase数据库中添加某些内容后发送推送通知我如何才能做到这一点,node.js,firebase,firebase-realtime-database,Node.js,Firebase,Firebase Realtime Database,我做这件事是参考上面的博客 以下代码用于在firebase中创建数据库: public static void sendNotificationToUser(String user, final String message) { DatabaseReference ref; ref = FirebaseDatabase.getInstance().getReference(); final DatabaseReference notificati

我做这件事是参考上面的博客

以下代码用于在firebase中创建数据库:

public static void sendNotificationToUser(String user, final String message) {
        DatabaseReference ref;
        ref = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference notifications = ref.child("notificationRequests");

        Map notification = new HashMap<>();
        notification.put("username", user);
        notification.put("message", message);

        notifications.push().setValue(notification);
    }
当我尝试部署它时,会出现如下错误:

分析触发器时出错:找不到模块“请求”

您是否安装了请求模块


您能告诉我如何提供服务帐户的路径吗?json您可以从Firebase网站上的项目设置下载此文件。例如,将此文件放在私人文件夹中,路径将为private/serviceAccountKey.jsoni给出的我的路径如下C:/Users/user/CSApplication/functions/private/google-services.json现在在解析函数触发器时出现了类似此错误的给出错误。对不起,我接受了你的答案。它在函数文件夹中,我用户如u所说,但它给了我一个错误:错误发生在解析函数触发器时。我建议你用上一期重新设置你的问题,并创建一个新问题,我想这篇文章的作者Frank van Puffelen会回答你的。嘿!在我解决第一个问题后,您无法更新您的问题!
var firebase = require('firebase-admin');
var request = require('request');

var API_KEY = ".AAAA6J9B3gg:APA91bEw_oHJSrAFuRxB6k6TzJ6wU-yzEw64yaf7CwBq7ur2iroGqi7Cf5JZ127wW_sBFtfdtBkpz15pPTVdQtkGPoMf6uBTMgq5AdTwXVWUDhtxQiGx5Pkj2CIpPDi0xTNT_ZMiEmIG";

var serviceAccount = require("path/to/serviceAccountKey.json");

firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: "https://chitchatapp-73060.firebaseio.com/"
});
ref = firebase.database().ref();

function listenForNotificationRequests() {
    var requests = ref.child('notificationRequests');
    requests.on('child_added', function (requestSnapshot) {
        var request = requestSnapshot.val();
        sendNotificationToUser(
            request.username,
            request.message,
            function () {
                requestSnapshot.ref.remove();
            }
        );
    }, function (error) {
        console.error(error);
    });
};

function sendNotificationToUser(username, message, onSuccess) {
    request({
        url: 'https://fcm.googleapis.com/fcm/send',
        method: 'POST',
        headers: {
            'Content-Type': ' application/json',
            'Authorization': 'key=' + API_KEY
        },
        body: JSON.stringify({
            notification: {
                title: message
            },
            to: '/topics/user_' + username
        })
    }, function (error, response, body) {
        if (error) { console.error(error); }
        else if (response.statusCode >= 400) {
            console.error('HTTP Error: ' + response.statusCode + ' - ' + response.statusMessage);
        }
        else {
            onSuccess();
        }
    });
}
listenForNotificationRequests();
npm install request