dongxingqiu7943 2017-05-11 19:53
浏览 36
已采纳

firebase messaging api php订阅主题411错误

I am trying to implement notifications into my web app. I have this php file in which I send notifications:

<?php
  function sendGCM($title,$message, $id) {
    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
        'registration_ids' => array (
                $id
        ),
        "notification" => array(
            "title" => $title,
            "body" => $message,
            "click_action" => "https://google.com"
        )
    );
    $fields = json_encode ( $fields );

    $headers = array (
        'Authorization: key=' . $MY_KEY,
        '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 );
  }
?>

Now what I want to do is to send a notification to a topic instead of individual ids. On this page it shows how you can subscribe an id to a topic. Here is the function I created to do that:

function createTopic($topic,$id) {
  $url = 'https://iid.googleapis.com/iid/v1/' . $id . '/rel/topics/' . $topic;
  $headers = array (
    'Authorization: key=MY_KEY'
  );
  $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 );

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

I am getting this error from Google and I can't figure out what is going wrong:

  1. That’s an error.

POST requests require a Content-length header. That’s all we know.

Any help?

  • 写回答

1条回答 默认 最新

  • dpqjvoq9033 2017-05-11 21:03
    关注

    The example in the documentation shows use of a Content-Length header:

    https://iid.googleapis.com/iid/v1/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA/rel/topics/movies
    Content-Type:application/json
    Content-Length: 0
    Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
    

    Add that to your request:

    $headers = array (
        'Authorization: key=' . $MY_KEY,
        'Content-Length: 0'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!