Firebase 通知:通过 HTTP Post 发送消息

Baw*_*ter 3 http push-notification ios firebase swift

我现在在我的应用程序中设置了 Firebase 通知,当我发送消息时,消息会发送给我应用程序的所有用户。(我目前通过 Firebase 控制台发送消息)。我想要另一种不涉及 Firebase 控制台的推送通知发送方式,我相信 HTTP Post 是一种简单的方法。如何使用 HTTP Post 远程发送推送通知?

Han*_*nes 9

编辑因为评论: 请确保你包括服务器密钥到您的客户端。有很多方法“不那么伟大的人”可以找到它并做一些事情......实现这一目标的正确方法是让您的客户端指示您的应用程序服务器发送通知。

您必须向 Google-API-Endpoint 发送 HTTP-Post。

您需要以下标题:

Content-Type: application/json
Authorization: key={your_server_key}
Run Code Online (Sandbox Code Playgroud)

您可以在 Firebase-Project 中获取您的服务器密钥。

HTTP 后内容:示例

{ 
    "notification": {
        "title": "Notification Title",
        "text": "The Text of the notification."
    },
    "project_id": "<your firebase-project-id",
    "to":"the specific client-device-id"
}
Run Code Online (Sandbox Code Playgroud)

示例设备 ID:

cc6VGMjpIiA:APA91bGLpm5Z2p0NNh7nxttCTVd1tTsL2jObDaS9U8G1YjDjkpwkBlRLjU89ns4ujQ7rFU1Z2NshpUAX2RiQiIDKhHJdB0RtSS3H6nTT-lGEkIpzVtVzJpLIVqzSVbRjmyYlxD3BSLZl
Run Code Online (Sandbox Code Playgroud)

您必须将此请求发送到https://fcm.googleapis.com/fcm/send。截至目前,无法使用 API 向所有设备发送通知。为此,您必须使用 Firebase-Console。


我喜欢使用 Chrome 插件“邮递员”发送 API 请求,因为您可以保存您的 HTTP 请求。它很舒服。

您也可以使用 curl。

curl 
-X POST 
-d "{ "notification": {
    "title": "Notification Title",
    "text": "The Text of the notification."
  },
  "project_id": "<your firebase-project-id",
  "to":"the specific client-device-id"
}" 
-H "Content-Type: application/json" 
-H "Authorization: key={your_server_key}" 
https://fcm.googleapis.com/fcm/send
Run Code Online (Sandbox Code Playgroud)