Dan*_*ona 4 javascript firebase react-native google-cloud-functions
我正在尝试编写一个 Firebase Cloud 函数来向用户显示推送通知。首先,我在 Firestore 数据库中创建通知,然后调用 Firebase 云函数向用户发送推送通知。问题是我真的不知道如何将参数传递给云函数
我这样调用该函数:
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) {
...
}
};
}
Run Code Online (Sandbox Code Playgroud)
然后,在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); });
}
Run Code Online (Sandbox Code Playgroud)
在我的云功能上index.js
exports.sendNotificationToUser = functions.https.onRequest((data, response) => {
console.log('Params:');
});
Run Code Online (Sandbox Code Playgroud)
如何将从文件发送的参数传递PushNotifications到相应的云函数?我自己对函数还很陌生
参数request, 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
});
Run Code Online (Sandbox Code Playgroud)
您还可以在请求正文中传递信息,然后通过request.body云函数访问它,但是您必须使用 POST 请求。
| 归档时间: |
|
| 查看次数: |
4468 次 |
| 最近记录: |