【问题标题】:I made everything right but firebase functions not working我把一切都做对了,但火力基地功能不起作用
【发布时间】:2019-07-17 02:09:09
【问题描述】:

我已经按照这些步骤一一应用了firebase函数

1- 我使用 android studio 连接到我的 firebase 并连接到云消息传递并安装了依赖项

2- 我通过终端安装了 firebase 工具并登录(在我编写此代码之前第一次无法使用)

alias firebase="`npm config get prefix`/bin/firebase"

3- 我在终端写firebase init 然后选择我的项目和javascript,然后是安装npm

4- 我确保我的 index.js 在函数文件夹中

5- 我用 sublime text 编写了这段代码来测试通知

'use strict'

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

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
    .onWrite(event =>{

        const receiver_id = event.params.receiver_id;
        const notification_id = event.params.notification_id;

        console.log('We have a notification to send to : ',  receiver_id);

        if(!event.data.val()){
            return console.log('A notification has been deleted from the database : ',notification_id);
        }

        const deviceToken = admin.database().ref(`/Users/${receiver_id}/deviceToken`).once('value');

        return deviceToken.then(result =>{
            const token_id = result.val();
            const payload = {

                notification:{
                    title: "Friend Request",
                    body: "you have received a new friend request",
                    icon: "default"
                }
            };

            return admin.messaging().sendToDevice(token_id, payload)
                        .then(response =>{
                            console.log('This was the notification feature.');
                        });
        });
    });

块引用

6- 然后我使用firebase deploy 进行部署并等待了 30 秒

7- 我在 2 台设备上安装了应用程序并清理了数据库。 最后,firebase 的函数部分没有显示任何内容

【问题讨论】:

  • 第一步是接受并接受这样一个事实,即您可能没有做对所有事情,否则它会起作用。
  • 好的,那么,你想从我们这里得到什么?如果您的代码和方法是完美的,您是否暗示 firebase 存在问题。如果您怀疑存在错误,请向相应的实体提交错误报告。但是,在这样做之前,您需要更详细的证据证明错误已经结束。
  • 您使用的是哪个版本的 Cloud Functions for Firebase。看来您使用的是旧语法,请参阅firebase.google.com/docs/functions/beta-v1-diff。您应该检查您的 package.json 文件并确认此语法与您的版本一致。 (无论如何,迁移到最新的 CF 版本是个好主意)
  • @RenaudTarnec,见上文。 OP 的意思是将他的评论指向你。
  • @KaremMohamed 我指的是 Cloud Functions 的版本,而不是 Cloud Messaging。最好的办法是与index.js 文件共享整个package.json 文件的内容。

标签: javascript android firebase google-cloud-functions


【解决方案1】:

你试过“firebase deploy --only functions”这个命令吗?

【讨论】:

  • @KaremMohamed 它以什么方式“不起作用”?请详细说明发生或未发生的事情与您预期发生的事情。请在本网站的帮助部分查看“如何提问”和其他常见主题。
  • 你的互联网连接良好吗?
猜你喜欢
  • 2020-03-25
  • 2019-08-25
  • 2016-09-17
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多