简体   繁体   English

无法发送推送通知

[英]Failed to send Push Notification

I try to send a message, but get nothing. 我尝试发送一条消息,但一无所获。 Although no fails, when printing the variable: 尽管没有失败,但是在打印变量时:

$result

this returns "to" (I have no idea why). 这返回“到”(我不知道为什么)。

The code I use is : 我使用的代码是:

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 and $registration_id must be an array to work with push notifications. $data$registration_id必须是一个数组,以使用推送通知。 so it should be like. 所以应该是这样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM