我想构建客户端和管理 Android 应用程序,以便管理应用程序向 FCM (API) 发送通知,并且客户端应用程序的用户会收到此通知。
因此,我使用 Firebase Admin SDK 通过 FCM documentation 将通知从管理应用推送到 FCM但接下来的代码中有一些奇怪的东西(来自文档)
// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
Message message = Message.builder()
.setNotification(new Notification(
"$GOOG up 1.43% on the day",
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))
.setCondition(condition)
.build();
// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
由于 send(RemoteMessage)
接受 RemoteMessage
而不是 Message
对象,因此我如何修改之前的代码来使用发送通知RemoteMessage
对象
最佳答案
您似乎正在尝试将 Firebase Admin SDK 与适用于 Android 的 Firebase Cloud Messaging SDK 混合使用。这是不可能的。
任何使用 Admin SDK 的进程都会被授予对您的 Firebase 项目的完全、无限制的访问权限。因此,如果您将其放入客户端应用程序中,则使用该应用程序的每个人都可以向您的任何用户发送 FCM 消息,而且还可以:列出所有这些用户、删除整个数据库、覆盖您的云功能等。原因是 Firebase Admin SDK 应该/只能在受信任的环境中使用,例如您的开发计算机、您控制的服务器或 Cloud Functions。
要通过 Firebase Cloud Messaging向设备发送消息,您始终需要有一个受信任的环境,在 FCM 文档中通常称为应用服务器。
当您在该受信任环境中运行 Admin SDK 时,您可以调用 FirebaseMessaging.getInstance().send() method ,它将 build()
返回的 Message
作为其参数。
另请参阅:
- 我的博文Sending notifications between Android devices with Firebase Database and Cloud Messaging
- Is it possible to send PushNotifications, using Firebase Cloud Messaging (FCM) to special UDID, directly from device?
- Firebase messaging without server XMPP server - Proposal
- How to send device to device messages using Firebase Cloud Messaging?
- 这个example of sending messages from Cloud Functions使用 Node.js
关于android - 使用 Android 应用程序向 FCM 发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55161851/