我正在尝试编写一个Firebase Cloud函数来向用户显示推送通知。首先,我在Firestore数据库中创建通知,然后调用Firebase Cloud函数向用户发送推送通知。问题是我真的不知道如何将参数传递给Cloud函数
我像这样调用函数:
import { sendNotificationToUser, sendNotificationToNonUser } from '../../api/PushNotification';
export function createNotification(values, callback) {
return async dispatch => {
try {
...
const newNotificationRef = firestore().collection('notifications').doc(notId);
const newNotification = await firestore().runTransaction(async transaction => {
const snapshot = await transaction.get(newNotificationRef);
const data = snapshot.data();
return transaction.set(newNotificationRef, {
...data,
...values,
id: notId,
viewed: false
});
});
if (newNotification) {
if (values.reciever === null) {
sendNotificationToNonUser(values.title, values.message);
...
} else {
sendNotificationToUser(values.title, values.message, values.reciever);
...
}
} else {
...
}
} catch (e) {
...
}
};
}然后,在PushNotification文档中,我有以下内容:
import axios from 'axios';
const URL_BASE = 'https://<<MyProjectName>>.cloudfunctions.net';
const Api = axios.create({
baseURL: URL_BASE
});
export function sendNotificationToNonUser(title, body) {
Api.get('sendNotificationToNonUser', {
params: { title, body }
}).catch(error => { console.log(error.response); });
}
export function sendNotificationToUser(title, body, user) {
Api.get('sendNotificationToUser', {
params: { title, body, user }
}).catch(error => { console.log(error.response); });
}在我的云函数index.js上
exports.sendNotificationToUser = functions.https.onRequest((data, response) => {
console.log('Params:');
});如何将我从PushNotifications文件发送的参数传递给相应的云函数?我自己也是函数新手。
发布于 2021-07-28 12:39:49
request, response参数(在本例中为data、response )本质上是Express Request and Response对象。您可以使用request的query属性来获取这些查询参数,如下所示。
exports.sendNotificationToUser = functions.https.onRequest((request, response) => {
console.log('Query Params:', request.query);
// This will log the params objects passed from frontend
});您也可以在请求正文中传递信息,然后在Cloud function中通过request.body访问它,但之后您必须使用POST请求。
https://stackoverflow.com/questions/68554074
复制相似问题