Okay, I think I’m well on the way to solving this Parse push notifications on Android in Corona business once and for all.
First, some clarity about what I think the Parse REST API docs are trying to tell us in it’s own special kind of way:
The REST API is meant for importing installations. Using it to create them is kind of a hack - especially on Android.
That out of the way, here’s how we “import” installations for Android devices (as they’re created in real time - you probably use the word “registration” for this, like normal people).
- You need your GCM Sender ID from the Play Developer Console and your GCM API Key from the Google Developer Console. If you don’t have either of these two things, you’ve missed a step. Do some Googling to get set up.
- Put this info in at Settings > Push > GCM Push Credentials on your Parse account.
- What you’ve done is basically linked Parse up to your GCM
Now, here’s where it gets interesting.
Add this code to your main.lua
print ("Registering for push notifications") local function notificationListener( event ) if ( event.type == "remote" ) then --handle the push notification print( "[PUSH] REC PUSH NOTIFICATION:" ) --require("pl.pretty").dump(event) elseif ( event.type == "remoteRegistration" ) then --code to register your device with the service print( "[PUSH] REMOTE REG" ) --require("pl.pretty").dump(event) local function onInstallation( event ) if not event.error then print( event.response.value ) end end --realistically, put a conditional here and only do this for android local installationObj = { ["deviceType"] = "android", -- important ["pushType"] = "gcm", -- important ["GCMSenderId"] = "XXX", -- important, and see my note below ["deviceToken"] = event.token, -- important ["channels"] = { "" }, -- you can change this if you need to } -- if you're not using mod\_parse, you should be. parse:createInstallation( installationObj, onInstallation ) end end
You’ve seen this code in the Corona Demo for Push Notifications, and it probably looks even more familiar if you’ve been staring blankly at the mod_parse push notifications documentation like have.
The most important thing to point out here is that GCMSenderId should be the same one you plugged into your settings in Parse.
What you’ve done essentially here is created a Push installation by assembling everything on-the-fly and importing it. The GCM Sender ID stuff is the missing link that we’ve all been trying to figure out.
When you run this (on an android device, or the android emulator targeting Google APIs and signed into a Google account, not the Corona Simulator), you should see some output that a record got created (if you have mod_parse debugging on). You can also pop into parse and look at the installations table to confirm this.
Now, fire up some curl and send a notification:
curl -X POST \ -H "X-Parse-Application-Id: XXX" \ -H "X-Parse-REST-API-Key: XXX" \ -H "Content-Type: application/json" \ -d '{ "channels": [""], "data": { "alert": "Test Push" } }' \ https://api.parse.com/1/push
You should get a push notification, that probably looks hideous and has no content, but it works. Im still working on figuring out how to get all the right pieces in the right places on the notification, but we’ve basically got end-to-end push notifications.
Using Parse.
On Android.
Using Coronoa.
*drops mic*
(Seriously, I’ve been at this for quite some time today. It’s time for bed. I’ll try to get the rest working and update. Feel free to beat me to it.)
One final note. I commented these out because I don’t know if anyone is using them, but the “Penlight” library is Lua gold. if nothing else, get yourself some “pl.pretty” for dumping tables, but theres lots of other useful stuff in there and you can pick and choose what you want.