【问题标题】:Firebase Notifications: Send Message Through HTTP PostFirebase 通知:通过 HTTP Post 发送消息
【发布时间】:2016-12-08 09:24:46
【问题描述】:

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

【问题讨论】:

    标签: ios swift http firebase push-notification


    【解决方案1】:

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

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

    您需要以下标题:

    Content-Type: application/json
    Authorization: key={your_server_key}
    

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

    HTTP-Post-Content:示例

    { 
        "notification": {
            "title": "Notification Title",
            "text": "The Text of the notification."
        },
        "project_id": "<your firebase-project-id",
        "to":"the specific client-device-id"
    }
    

    示例设备 ID:

    cc6VGMjpIiA:APA91bGLpm5Z2p0NNh7nxttCTVd1tTsL2jObDaS9U8G1YjDjkpwkBlRLjU89ns4ujQ7rFU1Z2NshpUAX2RiQiIDKhHJdB0RtSS3H6nTT-lGEkIpzVtVzJpLIVqzSVbRjmyYlxD3BSLZl
    

    您必须将此请求发送至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
    

    【讨论】:

    • 这确实是如何使用 FCM 发布消息的一个很好的例子。请注意,由于此方法需要使用您的 FCM 服务器密钥,因此您不应在客户端设备上运行此代码。在客户端内公开您的服务器密钥使恶意用户可以找到您的密钥并代表您发送消息。见stackoverflow.com/questions/37435750/…
    • 是的 - 这有点误导。我相应地编辑了我的帖子:)
    • 感谢您的回答!如果我想通过 Swift 代码发送这个请求,我该怎么做呢?
    • 我从来没有在 swift 代码中这样做过。必须有某种简化问题的 HTTP-Lib。对于 Java/Android,有 Volley ...您基本上必须提出相同的请求。 This might help! 祝你好运:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2017-07-03
    • 2021-09-02
    相关资源
    最近更新 更多