无法发送推送通知 [英] Failed to send Push Notification

查看:102
本文介绍了无法发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试发送消息,但什么都没有。虽然没有失败,但在打印变量时:

  $ result 

返回to(我不知道为什么)。



我使用的代码是: p>

  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; //表示结果OK
$ rtn [msg] =OK;
$ rtn [result] = $ result;
return($ rtn);


解决方案

$ data $ registration_id 必须是一个数组才能使用推送通知。所以应该是这样的。

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


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. so it should be like.

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

这篇关于无法发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆