android - Firebase Firestore 函数通知 Android

标签 android firebase notifications google-cloud-firestore google-cloud-functions

我需要有关 Firestore 功能的帮助。我正在开发一个需要通知的应用程序,并且我使用 firebase 作为后端,我对云功能特性完全陌生。

所以我想在将文档添加到集合时向用户发送通知,我已经尝试为这些功能设置一些东西,但由于我不知道的原因它不起作用,我的 Node Js 是更新版本, firebase 工具已更新,但我仍然遇到错误。

这是我的 index.js 文件和错误消息。感谢您的帮助。

'use strict'

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

exports.sendNotification = functions.firestore.document('Users/{userId}/Notifications/{notificationId}').onWrite((change, context) =>{

const userId = context.params.userId;
const notificationId = context.params.notificationId;

console.log('The User id is : ', userId);


if(!context.data.val()){

    return console.log('A notification has been deleted from the database');

}

// ref to the parent document
const device_token = admin.firestore.doc('Users/${userId}/Token/${userId}/deviceToken').once('value');

return device_token.then(result => {

    const token_id = result.val();

    const payLoad = {

    notification:{
        title: "Qubbe",
        body: "You have a new comment!",
        icon: "default"
    }

};

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

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

});

});
device_token.catch(error => {
    console.log(error);
});

});

错误: Scrrenshot to error in command line

最佳答案

感谢回复,问题已解决。

我首先删除了我的旧功能,然后重新开始,然后我在全局范围内安装了最新的 firebase 工具,并像通常在启动时一样更新 npm 工具。然后我将这段代码用于我的 firestore:

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



//const firestore = new Firestore();
//const settings = {/* Date... */ timestampsInSnapshots: true};
//firestore.settings(settings);

exports.sendNotification = 
functions.firestore.document('Users/{userId}/Notifications/{notificationId}')
.onWrite((change, context) =>{

const userId = context.params.userId;
const notificationId = context.params.notificationId;

console.log('The User id is : ', userId);
console.log('The Notification id is : ', notificationId);

// ref to the parent document

return admin.firestore().collection("Users").doc(userId).collection("Token").doc(userId).get().then(queryResult => {
    const tokenId = queryResult.data().deviceToken;

    //const toUser = admin.firestore().collection("Users").doc(userId).collection("Notifications").doc(notificationId).get();

        const notificationContent = {
                notification:{
                    title: "/*App name */",
                    body: "You have a new Comment!",
                    icon: "default",
                    click_action: "/*Package */_TARGET_NOTIFICATION"
            }
        };

        return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
            console.log("Notification sent!");
            //admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).delete();
        });

   });

});

关于android - Firebase Firestore 函数通知 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52240896/

相关文章:

javascript - WebView SpeedTest 结果始终低于 CustomTab 或 Chrome

android - 自定义 UI 控件指南

swift - 谁能帮我用 Swift 解决这个错误

android - 如何防止向 Wear 发送通知?

android - 膨胀抛出 OutOfResourcesException

android - textView 上的数据绑定(bind)空指针异常

java - 如何在每个用户 firebase android 中创建唯一的 token ?

arrays - 获取我的 Firebase 数据库的一部分中的所有子键并显示在 TableView 上

Android:在应用程序的设备中管理多个推送通知

android - 可以从工作线程调用 NotificationManager.notify() 吗?