GAS + GCM Message.Data Issue

50 views
Skip to first unread message

Clay Colwell

unread,
Apr 19, 2015, 10:21:01 PM4/19/15
to gcm-for-chr...@googlegroups.com
I've been beating my head against the wall for a couple of hours on this one. I have a Google Apps Script I am trying to use to send out Push Notifications via GCM. A single registration ID works fine, but trying to send a notification to several registration IDs leads to the data element of the message being empty.

var registrationIds = [];
 
 
//Get RegistrationIDs for matching Chrome instances
  registrationIds
.push('ID1');
  registrationIds
.push('ID2');

 
//Make payload
 
var payload = {
     
"data.msgType": "desktopNotification",
     
"data.type": "basic",
     
"data.title": form.notificationTitle, //String
     
"data.message": form.notificationMessage, //String
     
"data.icon": form.notificationIcon, //String
     
"data.buttons": btnCfg, //Array instantiated previously
     
"data.links": linkCfg, //Array instantiated previously
     
"delay_while_idle": true,
     
"registration_ids": registrationIds //Array of registration IDs from above
 
};
 
 
//Make params
 
var params = {
     
"headers" : {
       
"Authorization" : "key=" + apiKey, //Set the API KEy
     
},
     
"contentType": "application/json", //Set content type to be JSON
     
"method": "post",
     
"payload": JSON.stringify(payload) //Stringify the payload
 
};

  Logger.log(UrlFetchApp.getRequest(url, params)); //Debugging
  var response = UrlFetchApp.fetch(url, params); //Do submit


I get back expected response from this call. 2 success, 0 fails. I receive a back message_ids, and a multicast_id as well. However, when I take a look at the message.data on the client Chrome browser, the object is empty.

getRequest Results
[15-04-19 22:14:06:048 EDT] {headers={Authorization=key=$APIKEY$}, useIntranet=false, followRedirects=true, payload={"data.msgType":"desktopNotification","data.type":"basic","data.title":"Important Message","data.message":"","data.icon":"Alert","data.buttons":[],"data.links":[],"delay_while_idle":true,"registration_ids":["ID1","ID2"]}, method=post, validateHttpsCertificates=true, contentType=application/json, url=https://android.googleapis.com/gcm/send}

Response from fetch
[15-04-19 22:14:06:085 EDT] {failure=0, results=[{message_id=0:1429496046070775%fde00cbff9fd7ecd}, {message_id=0:1429496046070690%fde00cbff9fd7ecd}], success=2, multicast_id=7.1885521162196777E18, canonical_ids=0}


You'll see here the resulting message to the push on the client Chrome:

The first line is logging the raw message variable from the "hrome.gcm.onMessage.addListener(function(message) {})" listener. The second, empty object is message.data.

Any help would be greatly appreciated on this one. Thanks!

Filip Gorski

unread,
Apr 20, 2015, 5:07:36 PM4/20/15
to gcm-for-chr...@googlegroups.com
Hi, Clay,

Have you tried turning data into an object, which further uses msgType, type and so on as keys? Following this example:
{ "data": {
   
"score": "5x1",
   
"time": "15:10"
 
},
 
"registration_ids": ["4", "8", "15", "16", "23", "42"]
}
From Request format section of: http://developer.android.com/google/gcm/http.html

Please let me know.
Thanks.
Filip

Clay Colwell

unread,
Apr 21, 2015, 9:24:46 PM4/21/15
to gcm-for-chr...@googlegroups.com
That seems to have gotten it! I swear I had already tried that. Thanks so much!

In case anybody else has the issue in the future, here's what I did:

Change payload
//Make payload of data
  var payload = {
      "data": {
        "msgType": "desktopNotification",
        "type": "basic",
        "title": form.notificationTitle,
        "message": form.notificationMessage,
        "icon": form.notificationIcon,
        "buttons": btnCfg,
        "links": linkCfg
      },
      "delay_while_idle": true,
      "registration_ids": registrationIds
  };

Then make params
  //Make params
  var params = {
      "headers" : {
        "Authorization" : "key=" + apiKey,
      },
      "method": "post",
      "payload": JSON.stringify(payload),
      "contentType": "application/json"
  };



Filip Gorski

unread,
Apr 22, 2015, 1:35:42 PM4/22/15
to gcm-for-chr...@googlegroups.com
Great to hear. Please post here if you have more questions.

Thanks.
Filip
Reply all
Reply to author
Forward
0 new messages