微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 无法发送推送通知

我尝试发送消息,但一无所获.虽然没有失败,但在打印变量时:

$result

这会返回“to”(我不知道为什么).

我使用的代码是:

private function sendMessageGcm($registration_id,$message){             
  $this->key = "xxxxxxxxxxxxxxxxxxxxxx";
  $data = array(
    "registration_id" => $registration_id,
    "data" => $message
  );
  $headers = array(
    "Content-Type:application/json",
    "Authorization:key=" . $this->key
  );
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  $result = curl_exec($ch);
  if($result == false) {
    echo('Curl Failed: ' . curl_error($ch));
  }
  curl_close($ch);
  $rtn["code"] = "000";//means result OK
  $rtn["msg"] = "OK";
  $rtn["result"] = $result;
  return($rtn);
}

解决方法:

$data和$registration_id必须是一个用于推送通知的数组.所以应该是这样的.

$data = array(
    "registration_ids" => array($registration_id),
    "data" => array(
        "body" => $message,
    ),
);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐