onCreate时,我正在使用Firebase Cloud Functions进行推送通知。请帮忙。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/messages/diyetisyen/{uname}/{msgid}/message')
.onCreate((snapshot, context) => {
let message = snapshot.val();
let uname = context.params.uname;
//let uname_slice = uname.slice(0,(uname.length-4)) + uname.slice((uname.length-3));
let root = snapshot.ref.root;
let tokenRef = root.child('/users/' + uname + '/token').ref;
let payload = {
data: {
custom_notification: JSON.stringify({
body: message + 'a',
title: 'title'
})
}
};
let options = { priority: "high" };
return tokenRef.once('value')
.then(dataSnapshot => {
let token = dataSnapshot.val();
console.log('2 \n'+'message \n' + message +'\n tokenref \n' + tokenRef +'\n token \n' + token +'\n uname \n' + uname+'\n gönderiliyor');
return admin.messaging().sendToDevice(token, payload, options);
});
});
我正确获取消息,令牌,uname,但是
admin.messaging()。sendToDevice(令牌,有效载荷,选项);不起作用。
答案 0 :(得分:1)
我更改了有效负载
const payload = {
notification: {
title: 'title',
body: message + ''
}
};
它正在工作。