【发布时间】: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