如何使用 fcm api 请求消息?

IT技术 node.js reactjs firebase react-native firebase-cloud-messaging
2021-04-26 03:38:34

我正在使用与 firebase react-native来使用 fcm 推送通知..

这是文档网络示例

            // 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 来请求消息

我必须使用这个网址

这是

          https://fcm.googleapis.com/fcm/send

但我很困惑我该如何使用这个网址??

我想知道我应该在后端还是前端使用这个网址?

1个回答

通过 FCM 向设备发送消息需要您为 API 指定所谓的 FCM 服务器密钥。顾名思义,此密钥只能在受信任的环境中使用,例如您控制的服务器、您的开发机器或 Cloud Functions。

没有直接通过 FCM API 从客户端代码直接发送消息的安全方法。有关这方面的更多信息,请参阅: