Hey all!
I just got lucky and got my Corona code to work together with my Urban Airship account. This is just the code you will need to get in your main.lua file. How you setup the certificates and everything else at Urban Airship is an issue you will have to solve yourself.
In my main.lua file I first need to register the device to get the Device Token back and that device token has to be sent to Urban Airship to add the device to the list of potential customers.
I am writing an article on how to push from ASP.NET to Urban Airship but it will be later.
Remember to add in your APPKEY and APPLICATIONSECRET from Urban Airship into the variables below.
main.lua code for push
[lua]local mime = require( “mime” )
local json = require(“Json”)
– Here is the implementation of Push Notification in Corona SDK
local launchArgs = …
APPKEY = “”
APPLICATIONSECRET = “”
if launchArgs and launchArgs.notification then
– The code below will only trigger if your app is dead and not active at all
– *********************************************************************************
–native.showAlert( “launchArgs”, Json.Encode( launchArgs.notification ), { “OK” } )
–[[ notification table contains:
launchArgs.notification.type - “remote”
launchArgs.notification.name - “notification”
launchArgs.notification.sound - “sound file or ‘default’”
launchArgs.notification.alert - “message specified during push”
launchArgs.notification.badge - “5” – badge value that was sent
launchArgs.notification.applicationstate - “inactive”
–]]
native.showAlert( “Cocktails n Dreams”, launchArgs.notification.alert, { “OK” } )
end
– Function to handle Network Traffic Response from Urban Airship
local function urbanNetworkListener( event )
if ( event.isError ) then
–native.showAlert( “Network error!”, “Error has occured from Urban Airship”, {“OK”})
else
–native.showAlert( “Urban Airship”, event.response, {“OK”})
end
end
– Function to register device for Urban Airships Services
local function registerUrbanDevice(deviceToken)
local secretString = mime.b64(APPKEY … “:” … APPLICATIONSECRET)
headers = {}
headers[“Authorization”] = "Basic " … secretString
print("SecretString: " … secretString)
print("Device ID: " … deviceToken)
body = “”
local params = {}
params.headers = headers
params.body = body
network.request( “https://go.urbanairship.com/api/device_tokens/” … deviceToken, “PUT”, urbanNetworkListener, params)
end
– notification listener
local function onNotification( event )
if event.type == “remoteRegistration” then
registerUrbanDevice(event.token)
elseif event.type == “remote” then
– The code below will only trigger if your app is alive and kicking
– *********************************************************************************
–native.showAlert( “remote”, Json.Encode( event ), { “OK” } )
–[[ notification table contains:
launchArgs.notification.type - “remote”
launchArgs.notification.name - “notification”
launchArgs.notification.sound - “sound file or ‘default’”
launchArgs.notification.alert - “message specified during push”
launchArgs.notification.badge - “5” – badge value that was sent
launchArgs.notification.applicationstate - “inactive”
–]]
native.showAlert( “Cocktails n Dreams”, event.alert , { “OK” } )
end
end
– Initiera Eventhandler för Notifications
Runtime:addEventListener( “notification”, onNotification )
– Here is the end of the Push Notification[/lua] [import]uid: 22737 topic_id: 19522 reply_id: 319522[/import]
[import]uid: 52430 topic_id: 19522 reply_id: 75403[/import]