我已将下一个功能部署到了Firebase:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
console.log('FILE INCLUDED');
const serviceAccount = require('../cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://cargo-tender.firebaseio.com'
});
console.log('INITIALIZED');
export const onMessageCreate = functions.database
.ref('/chat')
.onCreate((snapshot, context) => {
console.log('onCreate RUN');
const token = 'myToken';
const payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default',
}
};
console.log('trying to send push notification');
return admin
.messaging()
.sendToDevice(token, payload).then((response) => {
console.log('Message sent');
console.log(response);
}).catch((error) => {
console.error('There was an error, while sending the message');
console.error(error);
});
});
console.log('FUNCTION FINISH');
但是,当我尝试在路径/user-chat
下创建记录时-未执行该函数,则未发送推送通知。另外,该函数调用未在日志中注册。
如果我从Firebase控制台手动运行该功能(测试功能)-该功能执行无错误。收到推送通知。而且我发现日志中的内容都很好:
3:07:56.902 PM onMessageCreate FILE INCLUDED
3:07:57.112 PM onMessageCreate INITIALIZED
3:07:57.113 PM onMessageCreate FUNCTION FINISH
3:07:57.273 PM onMessageCreate onCreate RUN
3:07:57.273 PM onMessageCreate trying to send push notification
3:07:57.757 PM onMessageCreate Message sent
3:07:57.759 PM onMessageCreate { results: [ { messageId: '0:1561550877745153%be2b376ebe2b376e' } ],
canonicalRegistrationTokenCount: 0,
failureCount: 0,
successCount: 1,
multicastId: 5684437879493655000 }
3:07:57.771 PM onMessageCreate Function execution took 1008 ms, finished with status: 'ok'
当我在Firebase中的指定路径(/user-chat
下添加一条记录时,我不明白为什么不触发onCreate的原因。我还尝试使用另一条路径,例如/chat
,但这并不能解决问题。
任何对此的建议,将不胜感激。
谢谢!
答案 0 :(得分:0)
已修复!
我必须加一个口罩,例如/user-chat/{anything}