我正在尝试编写一个Firebase Cloud函数来向用户显示推送通知。首先,我在Firebase数据库中创建通知,然后调用Firebase Cloud Function向用户发送推送通知。问题是我真的不知道如何将参数传递给云函数
我这样调用函数:
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); });
}
And on my Cloud Functionsindex.js
exports.sendNotificationToUser = functions.https.onRequest((data, response) => {
console.log('Params:');
});
我如何将我从PushNotifications
文件发送到相应的云函数的参数?我自己对函数还是个新手
request, response
参数(在您的示例中是data和response)本质上是Express Request和response对象。您可以使用请求的query
属性来获取这些查询参数,如所示。
exports.sendNotificationToUser = functions.https.onRequest((request, response) => {
console.log('Query Params:', request.query);
// This will log the params objects passed from frontend
});
您也可以在请求正文中传递信息,然后通过云函数中的request.body
访问它,但随后您必须使用POST请求。