So I finally got Push Notifications to work between Parse.com Cloud Service and Corona SDK. I have been struggling for days with matter so I thought I would share the code and steps I used to make it work. I did share it when I made it work with Urban Airship in 2011 and then it was very appreciated.
So, here we go…
-
Sign up with Parse.com
-
Create a new APP
-
Note down the APP ID key and the RESTFUL API key
-
Make sure you have the correct Mobile Provisioning files from Apple and that they support Push Notifications both development and production. Download them and export them to p12-files without passwords and upload them to the parse server under PUSH.
-
Make sure you have signed up with Google and enable the messaging service there to and note down the keys and especially the Projectnumber needed later. Enter all this information in the Parse config panel as well under PUSH, just like you did in step 4.
-
Make the below code insert in config.lua and make sure that you enter the correct number from the project noted from Google API. Make sure projectNumber is with capital N, spent a whole day with this
notification = { iphone = { types = { “badge”, “sound”, “alert” } }, google = { projectNumber = “000” }, },
-
If you want to support notifications when app is not alive you must also support launchargs so make sure you don’t forget to add that in the code as you can see below.
local function notificationListener(e) if e.type == ‘local’ then local data = e.custom or _G.emptyTable showAlert(data.title, data.message, {getText(‘option_ok’)}) end if (e.type == “remoteRegistration”) then local jsonEncode = require(‘json’).encode local platform = “ios” if e.token ~= nil then --save the deviceToken as a global so we can grab it later _G.deviceToken = e.token else print(“no token returned, too bad, check certificates”) end local myRegistrationId = e.token if (system.getInfo(“platformName”) == “Android”) then platform = “android” end local function networkListener(event) if (event.isError) then print(" ===>>> PARSE LISTENER ERROR RESPONSE : “, event.response) else print(” ===>>> PARSE LISTENER RESPONSE: “, event.response) end end local headers = { [“X-Parse-Application-Id”] = “your app id key here”, [“X-Parse-REST-API-Key”] = “your restful api key here”, [“Content-Type”] = “application/json” } local bodyData = { – These two fields must be added at parse.com first. [“deviceName”] = tostring( system.getInfo(“name”) ), [“deviceModel”] = tostring( system.getInfo(“architectureInfo”) ), [“deviceType”] = platform, [“deviceToken”] = _G.deviceToken, [“badge”] = native.getProperty(“applicationIconBadgeNumber”), [“timeZone”] = print(os.date(”%Z")), [“channels”] = {“your channels here”, “or more channels here”}, [“appName”] = “special app name used to filter push”, } local bodyMessage = jsonEncode(bodyData) local params = { headers = headers, body = bodyMessage } network.request( “https://api.parse.com/1/installations” ,“POST”, networkListener, params) elseif e.type == “remote” then native.showAlert( “Andreas Kviby”, e.alert , { “OK” } ) end end – Add event listener here Runtime:addEventListener(‘notification’, notificationListener) local launchArgs = … if ( launchArgs and launchArgs.notification ) then print( “event via launchArgs” ) notificationListener( launchArgs.notification ) end
You add the code above in your main.lua file and then make sure you build your app with the correct mobile provision certificate and try it? Deploy to device (NOT POSSIBLE TO TEST IN SIMULATOR!) and start the device and if everything goes well you can launch parse.com and send your first push in minutes!
So great to finally have everything working at last!