【问题标题】:Send Firebase Cloud Messages using HTTP protocol使用 HTTP 协议发送 Firebase 云消息
【发布时间】:2017-09-25 06:51:20
【问题描述】:

我想使用 FCM 为 Android、iOS 和 Web 构建一个聊天应用程序。我希望我的消息存储在我的服务器中,所以我的想法是将消息从我的应用程序发送到我的服务器,然后从我的服务器发送到使用 firebase 的其他应用程序用户。我发现了这种方法 (https://firebase.google.com/docs/cloud-messaging/http-server-ref) 来发送消息,但我的问题是,消息会使用 xmpp 传递到我的应用程序吗?传递消息会不会有额外的延迟?我可能有 50 人左右的聊天,会不会有问题?

【问题讨论】:

  • 即使您有数千或数百万的参与者也不会成为问题,并且您无需执行任何额外的操作,例如实现 xmpp 或任何其他协议 firebase,而 google 会管理这一点。
  • 不会有任何延迟。您可以使用两种协议:HTTP 或 XMPP

标签: android ios firebase firebase-cloud-messaging


【解决方案1】:

您可以在 PHP 中使用 curl 发送消息

function sendGCM($message, $id) {


    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
            'registration_ids' => array (
                    $id
            ),
            'data' => array (
                    "message" => $message
            )
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . "YOUR_KEY_HERE",
            'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );
}

?>

$message 是您要发送到设备的消息

$id 是设备注册令牌

YOUR_KEY_HERE 是您的服务器 API 密钥(或旧服务器 API 密钥)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2021-07-06
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多