代码之家  ›  专栏  ›  技术社区  ›  HemalHerath

我试图通过Firebase云函数发出通知,但我的代码没有发出任何通知

  •  0
  • HemalHerath  · 技术社区  · 7 年前

    const functions = require('firebase-functions')
    
    //initialize the app
    const admin = require('firebase-admin')
    admin.initializeApp(functions.config().firebase)
    
    const ref = admin.database().ref()
    
    //create user account function
    exports.createUserAccount = functions.auth.user().onCreate(event => {
        const uid = event.data.uid
        const email = event.data.email
    
        const newUserRef = ref.child(`/UserNotify/${uid}`)
        return newUserRef.set({
            email: email
        })
    })
    
    //delete user account function
    exports.cleanupUserData = functions.auth.user().onDelete(event => {
        const uid = event.data.uid
        const userRef = ref.child(`/UserNotify/${uid}`)
        return userRef.update({isDeleted: true}) 
    })
    
    function sendNotification() {
    
        console.log("Successfully sent");
    
        var payload = {
            notification: {
                title: "User get deleted",
                body: "sample@gmail.com"
            }
        };
    
        admin.messaging().sendToDeveice(payload)
            .then(function (response) {
                console.log("Successfully sent message:", response);
            })
            .catch(function (error) {
                console.log("Error sending message:", error);
            })
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Mecolela    7 年前

    admin.messaging().sendToDevice() 而不是 sendToDeveice

    检查: https://firebase.google.com/docs/cloud-messaging/admin/send-messages