Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试发送消息时gcm chrome中出现无效通道id错误_Javascript_Php_Google Chrome_Google Chrome Extension_Chrome Gcm - Fatal编程技术网

Javascript 尝试发送消息时gcm chrome中出现无效通道id错误

Javascript 尝试发送消息时gcm chrome中出现无效通道id错误,javascript,php,google-chrome,google-chrome-extension,chrome-gcm,Javascript,Php,Google Chrome,Google Chrome Extension,Chrome Gcm,我正在开发一个chrome扩展来接收来自Gcm推送服务的消息。 我开发了扩展来获取通道id,并且我有php代码来将消息发送到gcm服务器。php代码从gcm服务器返回错误,如下所示 错误:{错误:{错误:[{域:全局,原因:invalidParameter,消息:无效通道ID:{0},位置类型:参数,位置:通道ID}],代码:400,消息:无效通道ID:{0} 我已经向公众发布了chrome网络商店的chrome扩展。这里是链接。 将消息发送到gcm服务器的php代码 <html>

我正在开发一个chrome扩展来接收来自Gcm推送服务的消息。 我开发了扩展来获取通道id,并且我有php代码来将消息发送到gcm服务器。php代码从gcm服务器返回错误,如下所示

错误:{错误:{错误:[{域:全局,原因:invalidParameter,消息:无效通道ID:{0},位置类型:参数,位置:通道ID}],代码:400,消息:无效通道ID:{0}

我已经向公众发布了chrome网络商店的chrome扩展。这里是链接。

将消息发送到gcm服务器的php代码

<html>
<body>
<form name="pushform" method="POST" action="sendMessage.php">
Chrome Channel ID :<input type="text" name="channel" size="170" value="" />            
Message : <input type="text" name="message" size="100" maxlength="100" value
=" "/>
<input type="submit" value="Send Message" />
</form>
<?php

if(isset($_POST['channel']))
{
$channel = $_POST['channel'];
$message = $_POST['message'];

$url= 'https://accounts.google.com/o/oauth2/token';

//If you would have got refresh token
$refresh_token = "MY_REFRESH_TOKEN_FROM_GOOGLE"; //set ehre

$data = array();
$data['client_id']="MY_CLIENT_ID"; //set there
$data['client_secret']="MY_SECRET_CODE"; //set here
$data['refresh_token']=$refresh_token;
$data['grant_type']="refresh_token";



 // use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'method'  => 'POST',
    'content' => http_build_query($data),
),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$obj = json_decode($result);

if(!isset($obj->access_token))
{
die("ERROR: unable to get access token.\n".$result);
}

$access_token = $obj->access_token;


$url ="https://www.googleapis.com/gcm_for_chrome/v1/messages";
$data = json_encode(array(
    'channelId' => $channel,
    'subchannelId' => "1",
    'payload'=> $message
));

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => 
     "https://www.googleapis.com/gcm_for_chrome/v1/messages",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => $data,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_HTTPHEADER     => array(
        'Authorization: Bearer ' . $access_token,
        'Content-Type: application/json'
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
if(strstr($result,"error"))
{
    echo "ERROR:".$result;
}
else
    echo "Successfully sent message to Chrome";

}
?>
mymanifest.json文件

       {
     "app": {
      "background": {
     "scripts": [ "background.js" ]
       }
       },
      "description": "geting registration-id. ",


       "name": "First-test",
       "permissions": [  "gcm","notifications" ],
        "version": "2.0"
         }

需要在服务器端进行更多的日志记录。在它发出之前捕获/记录确切的卷曲请求..嗨!!谢谢你的回复……但是我没有收到你的回复……你的问题是你向服务器发送了一个无效的请求。该错误可能会有所帮助,但您确实需要在服务器发送整个请求以进行调试时捕获该请求。但它表示我从服务器获取的通道id无效…服务器还能说什么。。。
       {
     "app": {
      "background": {
     "scripts": [ "background.js" ]
       }
       },
      "description": "geting registration-id. ",


       "name": "First-test",
       "permissions": [  "gcm","notifications" ],
        "version": "2.0"
         }