invalid channel id error in gcm chrome when trying to send message via gcm server

11 views
Skip to first unread message

Sre Balaji

unread,
Mar 8, 2015, 10:08:45 AM3/8/15
to andro...@googlegroups.com
'm developing a chrome extension to receive messages from Gcm push services. I developed the extension to get the channel id and I have the php code to send the message to gcm server. The php code is returning error from gcm server like this...

   "ERROR:{ "error": { "errors": [ { "domain": "global", "reason": "invalidParameter", "message": "Invalid channel ID: {0}", "locationType": "parameter", "location": "channelId" } ], "code": 400, "message": "Invalid channel ID: {0}" } }"

I have published the chrome extension to chrome web store to public. Here is the link.https://chrome.google.com/webstore/detail/first-test/eooaplfpgeapcnniknheiodecggeipko

My php code to send message to gcm server..<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";

}
?>
My background.js file in my chrome extension is..
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].

   });
My manifest.json file
{
     "app": {
      "background": {
     "scripts": [ "background.js" ]
       }
       },
      "description": "geting registration-id. ",


       "name": "First-test",
       "permissions": [  "gcm","notifications"], 
"version": "2.0
}
Reply all
Reply to author
Forward
0 new messages