简体   繁体   中英

Getting Error=Missing Registration while sending HTTPPost to GCM Using Google Apps Script

I am trying to send http post to GCM server using google apps script. I have the device ID and the proper credentials. Here is the code I am using:

function doThat(){
  var url = "https://android.googleapis.com/gcm/send";
  var options = {
    "method": "post",
    "headers": {
      "Authorization": "key=myAPIKEY",
      "Content-Type":"application/json"
    },
    "payload": {
      "data": {
        "score": "5x1",
        "time": "15:10"
      },
      "registration_ids": ["regid1"]
    }
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log("Response: "+response.toString());
}

Here, myAPIKEY and regid1 are place holders. An equivalent java code works well for this. Hence there seems no problem with the credentials. I went through this link of android documentation to find the format of json to be sent to GCM server as(Those who edit this code below note that the api keys and others are directly taken from that link on android GCM site. ITS NOT MINE.SO PLS DO NOT CHANGE IT):

    Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : {
    ...
  },
}

Note: If I always get "Error=Missing Registration" at Logger.log() statement in execution logs. The java equivalent code works perfect. But google script gives this error.So what is wrong here?

I think you need to set the contentType advanced argument and stringify your payload. See this sample for pointers:

https://github.com/entaq/GoogleAppsScript/blob/master/Android/SharedDb.gs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM