2
votes

I'm trying to create a User Notification group on Google Cloud Messaging using a Google Apps Script server. I'm following directions here:http://developer.android.com/google/gcm/notifications.html

I'm getting the following error:

Request failed for https://android.googleapis.com/gcm/notification returned code 400. Truncated server response: {"error":"NOT_A_JSON_REQUEST"} (use muteHttpExceptions option to examine full response) (line 86, file "Code")

function SetUserNotification(data) {
  var url = "https://android.googleapis.com/gcm/notification";
  var apiKey = "<MyApiKey>";
  var projectID = "<MyProjectId>";
  var registrationId = data[2];

  var headers = {
      "Content-Type": "application/json",
      "project_id": projectID,
      "Authorization": "key=" + apiKey
    };
  var payload = { 
      "operation": "create",
      "notification_key_name": "ST-User" + data[3],
      "registration_ids": registrationId
   };
    var params = {
      headers: headers,
      method: "post",
      payload: payload
    };
    var response = UrlFetchApp.fetch(url, params);
  Logger.log("response = " + response.getContentText());
  return {message: "send completed: " + response.getContentText()};
  }

What am I doing wrong? I can't find any examples of creating a notification key online, just what seems to be incomplete instructions.

Update: Here's the execution transcript from the fetch function. Note line 87 is the fetch call.

[15-01-25 21:55:11:877 EST] UrlFetchApp.fetch([https://android.googleapis.com/gcm/notification, {headers={Authorization=key=MYAUTHKEY, project_id=MYPROJID, Content-Type=application/json}, payload={"operation":"create","notification_key_name":"ST-User1","registration_ids":"[abcdefgblahblah]"}, method=post}]) [0.015 seconds]

[15-01-25 21:55:11:879 EST] Execution failed: Request failed for https://android.googleapis.com/gcm/notification returned code 400. Truncated server response: {"error":"NOT_A_JSON_REQUEST"} (use muteHttpExceptions option to examine full response) (line 87, file "Code") [0.019 seconds total runtime]

2
In the documentation, "registration_ids" takes an array. You aren't formatting the data as an array. I'm not saying that's the issue, but it might be worth trying. "Hard code" it first to see if it makes a different. "registration_ids": ["4"]Alan Wells
Sandy, I've tried that and could not get it working. I'll look at fixing the payload variable later today to see if that's it.Scott
Corrected var, didn't work. You can see in my update that registration_ids comes through as an array.Scott
I'd put headers, method and payload in double quotes, and see if that makes a difference. "headers": headers, etcAlan Wells

2 Answers

2
votes

You need to to convert your payload to JSON, as this isn't done automatically. Do this using the javascript's built in JSON object as shown below.

Additionally, you cannot override the content-type header via the Headers parameter as you have in your code. You must use the separate contentType parameter instead.

var headers = {
  "project_id": projectID,
  "Authorization": "key=" + apiKey
};

var params = {
  headers: headers,
  method: "post",
  contentType: 'application/json',
  payload: JSON.stringify(payload)
};

See advanced parameters section here: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)

and JSON documentation here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON#Methods

0
votes

I found out!!!

function registerForKey(){
  var url = "https://android.googleapis.com/gcm/notification";
  var headers = {"content-type": "application/json","project_id": proj,    "Authorization": "key=" + api  };
  var request = {"operation": "create",   "notification_key_name": groupname,   "registration_ids": [id]};
  var pay = JSON.stringify(request);

  var params = {"headers": headers,"method": "POST",  "payload": pay }; 
  var respo = UrlFetchApp.fetch(url,params)
  
  Logger.log(respo);
  
  
  }

Please note a)in headers how the content type has to be written, with all minuscule and the trait between; b) payload needs to be stringified; c) insert a real id as request id, otherwise it will give an error!

So just copy this snippet, insert all the variables needed, run and then pres CTRL+Enter and...voila'! :)

Enjoy :D