Firebase cloud功能post方法应该是怎样的?



为什么不能运行函数?我的目标是向这个函数发送token,但是我无法进入这个函数。

firebase功能控制台没有输出(console.log(" is it Working ?")console.log("ReqBody: "+req)

我哪里做错了?

exports.sendNotifications = functions.https.onRequest((req, res) => {
if (req.method !== 'POST') {
return res.status(500).json({ message: 'Not allowed.' });
}
console.log("Is it Working ? ")
console.log("ReqBody : "+req)
var message = {
notification: {
title: 'New Message',
body: 'New Message!'
},
token: req.body.token
};
admin.messaging().send(message)
.then((response) => {

console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});

});

发送消息方法

sendMessage(){
let body = {
token : "token_string"
}
this.http.post(
'https://us-central1-project.cloudfunctions.net/sendNotifications',
body
);
}

当你为我添加订阅它的作品时,

sendMessage() (post method)

let body ="abc"
this.http.post<{s:any}>('https://us-central1-project.cloudfunctions.net/sendNotifications',
body
).subscribe(a=>console.log("This is work"));

添加订阅post方法后,我为admin添加了cors。消息传递工作

const cors = require('cors')({ origin: true });
exports.sendNotifications = functions.https.onRequest((req, res) => {
return cors(req, res, () => {
if (req.method !== 'POST') {
return res.status(500).json({ message: 'Not allowed.' });
}
console.log("Is it Working ? ")
console.log("ReqBody : "+req.body)
var message = {
notification: {
title: 'New Message',
body: 'New Message!'
},
token: "token"
};

admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
}) 
});

相关内容

  • 没有找到相关文章

最新更新