如何使用 fcm api 请求消息?
how can i request message by using fcm api?
我正在使用带有 firebase 的 react native 来使用 fcm 推送通知..
这是 documnet 示例
// Node.js
var admin = require('firebase-admin');
// ownerId - who owns the picture someone liked
// userId - id of the user who liked the picture
// picture - metadata about the picture
async function onUserPictureLiked(ownerId, userId, picture) {
// Get the owners details
const owner = admin
.firestore()
.collection('users')
.doc(ownerId)
.get();
// Get the users details
const user = admin
.firestore()
.collection('users')
.doc(userId)
.get();
await admin.messaging().sendToDevice(
owner.tokens, // ['token_1', 'token_2', ...]
{
data: {
owner: JSON.stringify(owner),
user: JSON.stringify(user),
picture: JSON.stringify(picture),
},
},
{
// Required for background/quit data-only messages on iOS
contentAvailable: true,
// Required for background/quit data-only messages on Android
priority: 'high',
},
);
}
文档说如果我想使用 rest api 而不是 firebase admin
来请求消息
我必须使用这个 url
这是
https://fcm.googleapis.com/fcm/send
但我很困惑如何使用这个 url??
我想知道我应该在后端还是前端使用这个 url?
通过 FCM 向设备发送消息需要您向 API 指定所谓的 FCM 服务器密钥。顾名思义,此密钥只能在受信任的环境中使用,例如您控制的服务器、您的开发机器或 Cloud Functions。
没有通过 FCM 直接从客户端代码直接发送消息的安全方法 API。有关更多信息,请参阅:
- Firebase 文档中的architectural overview
我正在使用带有 firebase 的 react native 来使用 fcm 推送通知..
这是 documnet 示例
// Node.js
var admin = require('firebase-admin');
// ownerId - who owns the picture someone liked
// userId - id of the user who liked the picture
// picture - metadata about the picture
async function onUserPictureLiked(ownerId, userId, picture) {
// Get the owners details
const owner = admin
.firestore()
.collection('users')
.doc(ownerId)
.get();
// Get the users details
const user = admin
.firestore()
.collection('users')
.doc(userId)
.get();
await admin.messaging().sendToDevice(
owner.tokens, // ['token_1', 'token_2', ...]
{
data: {
owner: JSON.stringify(owner),
user: JSON.stringify(user),
picture: JSON.stringify(picture),
},
},
{
// Required for background/quit data-only messages on iOS
contentAvailable: true,
// Required for background/quit data-only messages on Android
priority: 'high',
},
);
}
文档说如果我想使用 rest api 而不是 firebase admin
来请求消息我必须使用这个 url
这是
https://fcm.googleapis.com/fcm/send
但我很困惑如何使用这个 url??
我想知道我应该在后端还是前端使用这个 url?
通过 FCM 向设备发送消息需要您向 API 指定所谓的 FCM 服务器密钥。顾名思义,此密钥只能在受信任的环境中使用,例如您控制的服务器、您的开发机器或 Cloud Functions。
没有通过 FCM 直接从客户端代码直接发送消息的安全方法 API。有关更多信息,请参阅:
- Firebase 文档中的architectural overview