简体   繁体   English

使用 Firestore 中的 Firebase 云函数推送通知

[英]Push Notifications Using Firebase Cloud Functions in Firestore

I am having trouble making a javascript code that will send a notification to the user.我在制作将向用户发送通知的 javascript 代码时遇到问题。 I have few questions and clarifications that I want to know:我有几个问题和澄清我想知道:

  • Why am i having an error like this.为什么我有这样的错误。
TypeError: Cannot read property 'userID' of undefined
    at exports.sendNotification.functions.firestore.document.onWrite.event (/srv/index.js:8:30)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
  • And about the tokenID , how will i implement it?关于tokenID ,我将如何实现它? One time insertions when the user sign up?用户注册时一次性插入? or Every time the user logged in the token id will updated?还是每次用户登录令牌 id 时都会更新?

Below is my code on index.js:下面是我在 index.js 上的代码:

'use strict'

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

exports.sendNotification = functions.firestore.document("Buyers/{userID}/Notifications/{notificationId}").onWrite(event => {
    const userID = event.params.userID;
    const notificationId = event.params.notificationId;
    console.log("BuyerID: "+userID);


    return admin.firestore().collection("Buyers").doc(userID).collection("Notifications").doc(notificationId).get().then(queryResult => {
    const notificationMessage = queryResult.data().notificationMessage;
    const notificationTitle = queryResult.data().notificationTitle;

    console.log("Title: "+notificationTitle);    
    console.log("Message: "+notificationMessage);    

    const toUser = admin.firestore().collection("Buyers").doc(userID).get();

        return Promise.all(toUser).then(result => {
            const tokenId = result[0].data().tokenId;

            const notificationContent = {
                notification: {
                    title: notificationTitle,
                    body: notificationMessage,
                    icon: "default",
                    sound : "default"
                } 
            };

        return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
        console.log("Notification sent!");
        return null;
            });
        });
    })
});

And here is the sample structure of my database in firestore这是我在firestore中的数据库的示例结构在此处输入图像描述

You're using a very old syntax for Cloud Functions triggers.您正在为 Cloud Functions 触发器使用非常古老的语法。 The APIs changed since the beta release years ago.自几年前的 beta 版本以来, API 发生了变化。 Update your code to the newer signatures as shown in the documentation for Firestore triggers .将您的代码更新为较新的签名,如Firestore 触发器文档中所示。 Triggers now receive two arguments instead of one.触发器现在接收两个 arguments 而不是一个。

exports.sendNotification =
functions.firestore.document("Buyers/{userID}/Notifications/{notificationId}")
.onWrite((change, context) => {
    const userID = context.params.userID;
    const notificationId = context.params.notificationId;
    console.log("BuyerID: "+userID);

If you're following an old tutorial, please notify the author of the tutorial to update it.如果您正在学习旧教程,请通知该教程的作者进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM