尝试发送邮件时,gcm chrome中的通道ID错误无效

时间:2015-03-08 08:45:07

标签: javascript php google-chrome google-chrome-extension chrome-gcm

我正在开发Chrome扩展程序以接收来自Gcm推送服务的消息。 我开发了扩展以获取通道ID,我有php代码将消息发送到gcm服务器。 php代码正在从gcm服务器返回错误。

  

错误:{"错误":{"错误":[{"域名":"全球","原因& #34;:" invalidParameter"," message":"无效的频道ID:{0}"," locationType":&#34 ;参数"," location":" channelId" },"代码":400,"消息":"无效的频道ID:{0}" }}

我已将Chrome扩展程序发布到chrome网上商店。链接在这里。 https://chrome.google.com/webstore/detail/first-test/eooaplfpgeapcnniknheiodecggeipko

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

<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";

}
?>

我的chrome扩展程序中的background.js文件是。

var GCM_SENDERID = 'MY_SENDER_ID';
var GCM_SENDER = GCM_SENDERID + '@gcm.googleapis.com';

    // We don't expect errors, but let's be good citizens 
    and register error handlers

     var errorLogger = function() {
    console.error.apply(console, arguments);
     };
     chrome.gcm.onSendError.addListener(errorLogger);

     chrome.gcm.onMessagesDeleted.addListener(errorLogger);


    // This event handles incoming messages

     chrome.gcm.onMessage.addListener(function(msg) {
    // Who says comments don't get read?
     console.info('got GCM message', msg);

    // Incoming GCM data: we'll add callback here later [1].

   });


   // First things first, register with the GCM server at application start.

  chrome.gcm.register([GCM_SENDERID], function(regid) {
  if (chrome.runtime.lastError || regid === -1) {
  console.error(chrome.runtime.lastError);

  return;
 }

 console.info('registration id :, reg',regid);

  //chrome.app.window.create("pushhome.html?regid="+regid);
  window.open("pushhome.html?regid="+regid);
  // Connected OK: we'll add callback here later [2].

   });

我的manifest.json文件

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


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

0 个答案:

没有答案