hi, in the interest of science, I’ll put a theoretical fix up here and see if anyone can make it work. My problem is probably my testing environment. I have an un-activated Samsung II and a loaner Android that’s also not on a cell network, and I have a feeling that is stopping me getting the deviceID. That being said, I would think that the following would work (inspired by a post on the Parse forum).
-
Install Parse Cloud Code like thus: https://www.parse.com/docs/cloud_code_guide. It will install some files on your local, including one called main.js
-
In Main.js, add the following and deploy it up to your app’s cloud code:
[lua]Parse.Cloud.define(“sendPush”,function(request,response){
Parse.Cloud.httpRequest({
method: ‘POST’,
url: ‘https://android.googleapis.com/gcm/send’,
headers: {
‘Authorization’: ‘key=’ + ‘my_Google_API_Key’,
‘Content-Type’: ‘application/json’
},
body: {
registration_ids: request.params.deviceId,
data: “testing push notification on Android”
},
success: function(httpResponse) {
console.log(“success”);
},
error: function(httpResponse) {
console.log(‘GCM Request failed’ + JSON.stringify(httpResponse));
}
});
});[/lua]
- When you want to invoke the above code from your app, call it like this (you can use a REST API call to call any Parse cloud code function that you stick in min.'s:
–set the globals
APPID = ‘my_Parse_API_Key’
RESTAPIKEY = ‘my_Parse_REST_API_Key’
–pass through the device token you got back from GCM
[lua]
local function sendPush(dt)
local function sendPushListener()
print(“sent”)
end
headers = {}
headers[“X-Parse-Application-Id”] = APPID
headers[“X-Parse-REST-API-Key”] = RESTAPIKEY
headers[“Content-Type”] = “application/json”
local params = {}
commands_json =
{
[“deviceId”] = dt
}
postData = json.encode(commands_json)
local params = {}
params.headers = headers
params.body = postData
network.request( “https://api.parse.com/1/functions/sendPush”,“POST”,sendPushListener,params)
end[/lua]
This is all theoretical as my devices right now can’t get to first base (e.g. get the deviceID). But if someone else would like to run with this I think it would work. Look forward to your trials!
best,
Jen