Android 的 Firebase 云消息传递
Firebase Cloud Messaging with Android
我想将 Firebase 云消息传递与 Android 集成。我已经在 Firebase 上创建了我的帐户并与应用程序集成,并从 Firebase 控制台发送了一条消息。该应用程序收到了消息。到目前为止,这样的事情似乎有效。
我的问题是如何将 Firebase 与我自己的数据库服务器连接,该服务器由 MongoDB 组成,以检查要向其发送通知的用户的数据?有人可以指导我吗?
步骤很多,可能post到这里太长了,但既然你只是寻求指导,我会尽量运行完成所涉及的步骤。
- 您的 Android 应用程序需要将其 FCM 令牌发送到 运行 连接您的 MongoDB 实例的服务器。参考 this FCM 文档页面,您需要在
onTokenRefresh()
方法中实现 sendRegistrationToServer(refreshedToken);
。
- 您的服务器需要侦听客户端发送的 FCM 令牌。这可能是一个 HTTP 端点,用于侦听带有客户端令牌和用户名的 POST 请求。然后它可以将详细信息存储在您的数据库中。 (记得尽可能使用 HTTPS。)
- 启用云消息API:https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=insert-project-id-here
- 您可以使用 Firebase HTTP v1 API, but first you'll need to authorize your server to do so. Instructions here 从服务器 运行ning MongoDB 发送 FCM 消息。您将获得需要包含在所有 API 请求的 HTTP header 中的授权令牌。
- 获得有效令牌后,您可以向
https://fcm.googleapis.com/v1/{parent=projects/*}/messages:send
发送 HTTP POST 请求,其中 body 包含消息的详细信息。 This page contains a description of the API endpoint. This page 包含要发送的消息 body 的描述。
作为 POST 请求的简短示例(目前未经测试)。如果您的项目 ID 是 fcm-test-12345
,您将向 POST 发出请求:
https://fcm.googleapis.com/v1/projects/fcm-test-12345/messages:send
header 应包含:
Authorization: Bearer <your access token>
Content-Type: application/json
而 body 会是这样的:
{
"message": {
"data": { "key": "value" }, // optional data to send to client.
"notification": {
"title": "Notification title",
"body": "Notification body"
},
"token": "<client device token from MongoDB>"
}
}
我想将 Firebase 云消息传递与 Android 集成。我已经在 Firebase 上创建了我的帐户并与应用程序集成,并从 Firebase 控制台发送了一条消息。该应用程序收到了消息。到目前为止,这样的事情似乎有效。
我的问题是如何将 Firebase 与我自己的数据库服务器连接,该服务器由 MongoDB 组成,以检查要向其发送通知的用户的数据?有人可以指导我吗?
步骤很多,可能post到这里太长了,但既然你只是寻求指导,我会尽量运行完成所涉及的步骤。
- 您的 Android 应用程序需要将其 FCM 令牌发送到 运行 连接您的 MongoDB 实例的服务器。参考 this FCM 文档页面,您需要在
onTokenRefresh()
方法中实现sendRegistrationToServer(refreshedToken);
。 - 您的服务器需要侦听客户端发送的 FCM 令牌。这可能是一个 HTTP 端点,用于侦听带有客户端令牌和用户名的 POST 请求。然后它可以将详细信息存储在您的数据库中。 (记得尽可能使用 HTTPS。)
- 启用云消息API:https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=insert-project-id-here
- 您可以使用 Firebase HTTP v1 API, but first you'll need to authorize your server to do so. Instructions here 从服务器 运行ning MongoDB 发送 FCM 消息。您将获得需要包含在所有 API 请求的 HTTP header 中的授权令牌。
- 获得有效令牌后,您可以向
https://fcm.googleapis.com/v1/{parent=projects/*}/messages:send
发送 HTTP POST 请求,其中 body 包含消息的详细信息。 This page contains a description of the API endpoint. This page 包含要发送的消息 body 的描述。
作为 POST 请求的简短示例(目前未经测试)。如果您的项目 ID 是 fcm-test-12345
,您将向 POST 发出请求:
https://fcm.googleapis.com/v1/projects/fcm-test-12345/messages:send
header 应包含:
Authorization: Bearer <your access token>
Content-Type: application/json
而 body 会是这样的:
{
"message": {
"data": { "key": "value" }, // optional data to send to client.
"notification": {
"title": "Notification title",
"body": "Notification body"
},
"token": "<client device token from MongoDB>"
}
}