Corona SDK Push Notifications and Urban Airship SOLVED

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]

Wow, this is surprisingly very straight-forward.

Thanks for sharing this! I’m sure many will find it very useful :slight_smile: [import]uid: 52430 topic_id: 19522 reply_id: 75403[/import]

Great work! This is a lot prettier than the equivalent ObjC IMHO. [import]uid: 7079 topic_id: 19522 reply_id: 75510[/import]

Great Job! [import]uid: 110373 topic_id: 19522 reply_id: 75539[/import]

Sorry, been under a rock and this is the first time I hear of Urban Airship. Looks great, would this be a viable solution to android in-app purchase? [import]uid: 80305 topic_id: 19522 reply_id: 75566[/import]

This is good stuff but unless I’m missing something without the ability to do a custom AndroidManifest file with the required C2DM settings the above code will only work for iOS. Seems a shame since both products are such good cross platform solutions otherwise. [import]uid: 62031 topic_id: 19522 reply_id: 76218[/import]

Many thanks. I was so excited when I found out we could do Push, then my heart sank a bit when I thought I needed my own server… This is great! [import]uid: 61520 topic_id: 19522 reply_id: 79469[/import]

Thanks…I was just working on a way to do this, you’ve saved me some time :slight_smile: [import]uid: 8697 topic_id: 19522 reply_id: 79472[/import]

I am glad that a lot of people appreciate my code. Should I publish the .NET project to send the actual notifications as well maybe? [import]uid: 22737 topic_id: 19522 reply_id: 79474[/import]

@akviby Yes! That would be great if you could do that :slight_smile: I’d really like a full tutorial on UA and Push, since I don’t know anything about web dev or servers etc. What you’ve posted is already a great start! Thanks [import]uid: 51654 topic_id: 19522 reply_id: 79488[/import]

I set up an Urban Airship free account and demo project with the following push mode:

“In development, connecting to sandbox push servers”

(all of my Apple certificates are in order)

I then created a Corona app with your code using my App Key Urban codes but nothing happens when executed on my phone.

What am I missing?

What should I be seeing as a notification?

Any help would be appreciated. [import]uid: 22152 topic_id: 19522 reply_id: 86345[/import]

Hey
I am on My mobile but look at the blogpost under blogs on how to setup the app to register the device in config.lua and or build.settings [import]uid: 22737 topic_id: 19522 reply_id: 86347[/import]

I changed the config.lua files as outlined in the blog. but still nothing.

I do get the following error in the console:
Feb 11 16:26:30iPhone4S SpringBoard[15] : No valid ‘aps-environment’ entitlement string found for application ‘PushDemo’: (null). Notifications will not be delivered.
Feb 11 16:26:30iPhone4S UIKitApplication:com.autolib.executiveceo[0xe00d][19979] : Lua Runtime Error: lua_pcall failed with status: 2, error message is: ?:0: attempt to concatenate a nil value
Feb 11 16:26:31 iPhone4S kernel[0] : launchd[19979] Builtin profile: container (sandbox)
Feb 11 16:26:31 iPhone4S kernel[0] : launchd[19979] Container: /private/var/mobile/Applications/F87139A6-D378-4BCF-AE23-4758B87775EB [69] (sandbox)

Any ideas?
[import]uid: 22152 topic_id: 19522 reply_id: 86355[/import]

Have you downloaded the new certificates from Apple and restarted Corona ? [import]uid: 22737 topic_id: 19522 reply_id: 86371[/import]

Yes.

I have a Basic Urban Airship plan does that matter?

The following are my Airship settings:
[blockcode]
ktvWQxBaQL6IZdxxNtstGg
Application Secret
Click to show
Application Master Secret
Click to show
Application Mode
Development, connecting to test servers.
Push Notifications support
In development, connecting to sandbox push servers.
Push certificate status
Ready for use
Push certificate bundle ID
com.autolib.executiveceo
Push debug mode
On
Allow push from device
off
BlackBerry Username
Not set!
Android Package
Not set!
C2DM Auth Token
Not set!
Device Tokens
1
Active Device Tokens
1
Active Users (month)
1
Pushes Sent (month)
2
Pushes Sent (all time)
2
Subscriptions (month)
0
In-App Purchase support
Disabled. No In App Purchase integration.
Rich Push Enabled
No
Subscriptions Enabled
Yes
Statistics not in real time.
You can also delete this app.
[/blockcode]

I appreciate your time looking at this.

[import]uid: 22152 topic_id: 19522 reply_id: 86378[/import]

I have a small question myself, after receiving a notification, do we need to do anything special to clear the little 1 notification on the app’s icon?

I’ve clicked on the OK in the message in the app, I’ve cleared my notifications from the drop down notification centre, and still a little 1 taunting me :slight_smile:

For autolib: Have you tried using an adhoc provision and the production servers? I went that route and had no problems. Also do you get the notice when you launch your app to allow push notifications?

Thanks [import]uid: 54327 topic_id: 19522 reply_id: 86380[/import]

I do not get the notice about Push Notifications. I backed off using production servers because they wanted a credit card. Do you think that is the only way it will work? [import]uid: 22152 topic_id: 19522 reply_id: 86382[/import]

I had some trouble getting notifications allow to pop up at first do I just did a blank app Im also out on my cell now. When I get back I’ll drop you what I used or email me jon@htwgames.com and I’ll send you what I used as a template. Once I had that going I just dropped those bits into our app to play around further. As for the dev vs production. I can’t say I just went straight to adhoc so I could share with my team. [import]uid: 54327 topic_id: 19522 reply_id: 86386[/import]

Solved my problem, it is when I do a send broadcast and stick a # in the badge field :slight_smile: [import]uid: 54327 topic_id: 19522 reply_id: 86403[/import]

I want to push a message to the Urban Airship server written in Lua such as the example below in Curl:

-X POST -u “:” \
-H “Content-Type: application/json” \
–data ‘{“device_tokens”: [""], “aps”: {“alert”: “Hello!”}}’ \
https://go.urbanairship.com/api/push/url:

Basically is it possible to have a button in a Corona app push code like above to a sever and then the server, Urban Airship for example, in turn will send the message? [import]uid: 22152 topic_id: 19522 reply_id: 87650[/import]