【问题标题】:Firebase Firestore Functions Notification AndroidFirebase Firestore 函数通知 Android
【发布时间】:2019-02-13 21:22:57
【问题描述】:

我需要有关 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

【问题讨论】:

  • 我已经在我的一个 tutorials 中逐步说明了如何使用 Cloud Firestorenotifications 发送给特定用户和Node.js。你也可以看看我这个 post 的回答。

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


【解决方案1】:

感谢回复,问题已解决。

我首先删除了我的旧功能,然后重新开始,然后我在全球范围内安装了最新的 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();
        });

   });

});

【讨论】:

    【解决方案2】:

    由于您使用以下语法onWrite((change, context)...),因此我假设您使用的 Cloud Functions 版本 >= 1.0。

    对于 >= 1.0 的版本,您应该在不带任何参数的情况下初始化 firebase-admin,如下所示:

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

    https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin

    【讨论】:

      猜你喜欢
      • 2020-02-08
      • 2018-02-21
      • 2018-09-29
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 2019-06-24
      • 2017-05-04
      相关资源
      最近更新 更多