Having a nightmare with push notifications. Please help!

Hi all,

First of all, I couldn’t find a section in “3rd party tools” for PooshWoosh… Pleeeeze (mods), don’t put this thread in 3rd party tools/other. No one ever looks in there.

Right, today, I thought I’d try the push woosh integration into iOS. I’ve spent all day, repeating the various processes and I just can’t get it to work. I mean, it does nothing. Nada.

I’ll just list the steps I went through. If anyone can see something glaringly obvious, that I’ve missed or messed up, let me know. I made a promise to my Client that this would be an easy process. There’s a distinct possibility, that I could end up looking stoopid!

This is what I did.

1/ iOS Dev center: Configured my app ID for push

2/ Downloaded the SSL. Installed into keychain (has green tick and is associated with key)

Note* This is a development app and pushes are in sandbox.

3/ Created the main development cert (associated with correct app ID)

4/ Created development provisioning profile using dev cert from step 3

5/ Installed provisoning profile into X-Code (is valid)

6/ Opened and set up app on PushWoosh

7/ Imported ssl cert and previously exported private key into Poosh Woosh

8/ Got app ID from pushwoosh

9/ Included the following code into my config

notification = { iphone = { types = { "badge", "sound", "alert" } } } 

10/ Included this code into my main.lua   --(this is Corona’s code example, NOT pushWoosh (some slight differences, although I have tried them both)

local json = require "json" local function notificationListener( event ) if ( event.type == "remote" or event.type == "local" ) then --handle the push or local notification elseif ( event.type == "remoteRegistration" ) then local deviceToken = event.token local deviceType = 1 --default to iOS if ( system.getInfo("platformName") == "Android" ) then deviceType = 3 end print( "Setting up with PushWoosh" ) local DeviceID = event.token local PW\_APPLICATION = "XXXXX-EE7C8" --use your app ID in PushWoosh local PW\_URL = "https://cp.pushwoosh.com/json/1.3/registerDevice" local function networkListener( event ) if ( event.isError ) then --error occurred native.showAlert( "Notification Registration Failed", "An Error Contacting the Server has Occurred.", { "OK" } ) else --registration successful! print( "PushWoosh registration successful", system.getInfo("deviceID") ) end end local commands\_json = { ["request"] = { ["application"] = PW\_APPLICATION, ["device\_id"] = DeviceID, ["language"] = "en", --OR: system.getPreference( "ui", "language" ), ["hw\_id"] = system.getInfo("deviceID"), ["timezone"] = -3600, --offset in seconds ["device\_type"] = deviceType } } local post\_body = json.encode( commands\_json ) local headers = {} headers["Content-Type"] = "application/json" headers["Accept-Language"] = "en-US" local params = {} params.headers = headers params.body = post\_body network.request ( PW\_URL, "POST", networkListener, params ) end end Runtime:addEventListener( "notification", notificationListener )

11/ Built my app using provisioning profile (no issues)

12/ compressed to IPA, uploaded to testFlight, installed on device, sent push note from PooshWoosh.

13/ Nothing!!! Not even an angry alert.

Can somebody, please, let me know if I’ve missed something obvious, point me in the direction of some working code or at least a walkthrough that’s more idiot-friendly, than these:

http://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/corona-sdk-integration/

http://docs.coronalabs.com/guide/events/appNotification/index.html

…Driving me insane

Additional… I swapped back to the original code provided by push Woosh and it’s now working, albeit, not totally predictably.

1/ The badge doesn’t dissapear when you click the push and/or app.

2/ Nothing happens at all if the app is already open

3/ If the app is active but not ‘in focus’, the push is visible at the top of the screen briefly, but no sign of the whole message when you open the app.

4/ The only time I get a notification, which then displays as a proper alert (within the app), is if I open the app from cold start/off.

I’ll keep on top of this thread. But for anyone who’s testing, make sure you suspend, minimise or quit your app. I imagine there’s lots of people, staring at their open app, wondering why nothing’s happening.

Hi @Caithan Games

First, we don’t officially support PushWoosh, however their service does seem to work with Corona SDK.  We used PushWoosh as an example in a blog post long ago (perhaps where you got the code) but because we don’t officially support, it, PushWoosh has likely made changes in how their service works and we’ve not updated the place you got that code from.

  1. You have to clear the badge yourself.  You use the:  native.setProperty( “applicationIconBadgeNumber”, badge_num ) api call.  See our Notification guide: http://docs.coronalabs.com/guide/events/appNotification/index.html

  2. When your app is open, you get an event.type == “remote” when you receive a notification event.   Your code above does nothing when you get that event.

  3. If the app is backgrounded and you’re on your device, you will get a notification across the top.  If you interact with the notification, it should bring your app into the foreground and you will get a notification event of type == “remote”.  It’s your responsibility to do something at that point.

  4. If you app is not running you are cold-started in which case the launchArgs is set with the push information.  All of this is covered in the guide I posed above.

Rob

Thanks Rob. The more time I’ve spent with it, the more I’ve managed to get my head around it… I completely get ‘removing the badge’, I did manage to find that snippet somewhere.

I know you don’t support pushWoosh, and I really do appreciate you taking the time to help, it seems like a lot of my issues are based around my understanding of JSON.

I added the elseif event.type == “remote”, and I’m now getting the alert that is scoped in their demo code, which brings me to the last thing I don’t understand. The code looks like;

local json = require "json" if launchArgs and launchArgs.notification then native.showAlert( "launchArgs", json.encode( launchArgs.notification ), { "OK" } ) end -- notification listener local function onNotification( event ) if event.type == "remoteRegistration" then local PushToken = event.token local PW\_APPLICATION = "006F9-EE7C8" local PW\_URL = "https://cp.pushwoosh.com/json/1.3/registerDevice" local deviceType = 1 if ( system.getInfo("platformName") == "Android" ) then deviceType = 3 end local function networkListener( event ) if ( event.isError ) then native.showAlert( "Notification Registration Failed", "An Error Contacting the Server has Occurred. Please try again later from the application settings.", { "OK" } ) else end end local commands\_json = { ["request"] = { ["application"] = PW\_APPLICATION, ["push\_token"] = PushToken, ["language"] = system.getPreference("ui", "language"), ["hwid"] = system.getInfo("deviceID"), ["timezone"] = 3600, ["device\_type"] = deviceType } } local jsonvar = {} jsonvar = json.encode(commands\_json) local post = jsonvar local headers = {} headers["Content-Type"] = "application/json" headers["Accept-Language"] = "en-US" local params = {} params.headers = headers params.body = post network.request ( PW\_URL, "POST", networkListener, params ) elseif event.type == "remote" then native.showAlert( "remote", json.encode( event ), { "OK" } ) end end Runtime:addEventListener( "notification", onNotification )

So I’m now getting a launchArg alert, which is great, but it comes with a load of JSON encoding that I can’t caorrelate to any part of my code, so I guess the json.encode call brings this ‘extra’ data with it… The alert looks like this:

{“badge”:3,“type”:“remote”,“name”:“notification”,“custom”:“sound”:“default”,“alert”:“this is my actual puch notification”,“applicationState”:“inactive”}

Obviously, I just want my notification, not the spec’ data, that I can only assume comes from ‘local headers’ (guessing)… Any idea how I can just extrapolate the push and ommit this status-data? It’s all Chinese to me.

Thanks

launchArgs is a Lua table that has some things in it.  The thing you are interested in with a push notifications is it’s member launchArgs.notification, which is another Lua table.  The contents of that notification table includes:

launchArgs.notification.badge  – a number

launchArgs.notification.type – a sting that will always be “remote”

launchArgs.notification.name – a string that will be “notification”

launchArgs.notification.alert – a string with the text message sent along with the push

launchArgs.notification.applicationState – a string that lets you know the state of your app when the notification was received.  In your example the value is “inactive” meaning the app was code started.  Since this is part of launchArgs, it will always be “inactive” if the app was code started.

launchArgs.notification.custom – a table of custom data.  In this case it has a single member:   sound = “default”.

The json.encode() there is a simple way of taking a Lua table and turning it into a single string that can be easily printed (or in this case with native.showAlert()).  The example isn’t doing anything with JSON other than making the table printable.

If you read the documentation for the notification event that is triggered through the event handler, you will notice that those members (badge, type, name, alert, applicationState, custom) are all the members of the event table passed to the notification event.

In your event handler you have another native.showAlert() that’s taking the event table and turning into a printable string using json.encode.  If it helps you instead of doing the native.showAlert that you have, you could do:

native.showAlert(event.alert, {“Okay”})

To help you with the output from JSON to make it something you might understand a little better.  Simply put it so that there is a line break where there are commas:

{“badge”:3,“type”:“remote”,“name”:“notification”,“custom”:“sound”:“default”,“alert”:“this is my actual puch notification”,“applicationState”:“inactive”}

becomes

{

  “badge”:3,

  “type”:“remote”,

  “name”:“notification”,

  “custom”:“sound”:“default”,

  “alert”:“this is my actual puch notification”,

  “applicationState”:“inactive”

}

Now take the : and put in an =

{

  “badge” = 3,

  “type” = “remote”,

  “name” = “notification”,

  “custom” = “sound”:“default”,

  “alert” = “this is my actual puch notification”,

  “applicationState” = “inactive”

}

It’s starting to look more like a Lua table right?  Now we just have to take the quotes off the key and assign it to a table.

event =

{

  badge = 3,

  type = “remote”,

  name = “notification”,

  custom = “sound”:“default”,

  alert = “this is my actual puch notification”,

  applicationState = “inactive”

}

Now it is a Lua table.  JSON isn’t that hard to read.

Rob

Dear All, 

I have started using PushWhoosh with Corona and more or less have it working. 

I am getting push notifications and

  1. if the app is open we get event.type == “remote” event
  2. if the app is not open, we get a tray notification and on clicking it the app starts and we get a handle on the notification contents again (as above)

I do have 1 scenario which is a bit puzzling. Say there is a push notification that has arrived (the app is not open), now if the user does not view the notification but instead opens the app directly, I am unable to get the message which the notification has sent. 

Any pointers on this will be much appreciated.

Cheers,

Anshuman

Anshuman, unfortunately that’s the way push notifications work.  If you invoke the app on it’s own, it has no way of knowing about the push.

Rob

Thanks for your reply Rob. Apparently if I clear off the notifications from the tray, and then open the App, even then the app doesnt get those notifications. 

This has thrown a spanner in the works, as now the chat is no longer “reliable”, and possibly the recipient will miss the chat unless they have the app already open or click on the notification to open the app.

Any suggestions on how to implement a resilient chat system would be much appreciated!

Cheers,

Anshuman 

You should use some type of on line server that actually take care of receiving messages.  When the user connects, it can fetch the last X messages and compare that with messages it’s received and go that way.  Push is not really a good way to do chat.  Push is more of a way to remind people who are not in your app to get a reminder to them to do something.  Clearing the push list from the OS never activates the app.  Launching the app from the icon also doesn’t communicate with the push.

Thanks Rob, I have now gone down that route. Have written a nodejs powered chat API for ‘chat sync’ whenever the user opens the apps (or on App Resume event), which syncs any new chats on server to local app DB. Live chats are still via push notifications exchange, but will consider using socket connections or pub-sub maybe. 

Cheers

Additional… I swapped back to the original code provided by push Woosh and it’s now working, albeit, not totally predictably.

1/ The badge doesn’t dissapear when you click the push and/or app.

2/ Nothing happens at all if the app is already open

3/ If the app is active but not ‘in focus’, the push is visible at the top of the screen briefly, but no sign of the whole message when you open the app.

4/ The only time I get a notification, which then displays as a proper alert (within the app), is if I open the app from cold start/off.

I’ll keep on top of this thread. But for anyone who’s testing, make sure you suspend, minimise or quit your app. I imagine there’s lots of people, staring at their open app, wondering why nothing’s happening.

Hi @Caithan Games

First, we don’t officially support PushWoosh, however their service does seem to work with Corona SDK.  We used PushWoosh as an example in a blog post long ago (perhaps where you got the code) but because we don’t officially support, it, PushWoosh has likely made changes in how their service works and we’ve not updated the place you got that code from.

  1. You have to clear the badge yourself.  You use the:  native.setProperty( “applicationIconBadgeNumber”, badge_num ) api call.  See our Notification guide: http://docs.coronalabs.com/guide/events/appNotification/index.html

  2. When your app is open, you get an event.type == “remote” when you receive a notification event.   Your code above does nothing when you get that event.

  3. If the app is backgrounded and you’re on your device, you will get a notification across the top.  If you interact with the notification, it should bring your app into the foreground and you will get a notification event of type == “remote”.  It’s your responsibility to do something at that point.

  4. If you app is not running you are cold-started in which case the launchArgs is set with the push information.  All of this is covered in the guide I posed above.

Rob

Thanks Rob. The more time I’ve spent with it, the more I’ve managed to get my head around it… I completely get ‘removing the badge’, I did manage to find that snippet somewhere.

I know you don’t support pushWoosh, and I really do appreciate you taking the time to help, it seems like a lot of my issues are based around my understanding of JSON.

I added the elseif event.type == “remote”, and I’m now getting the alert that is scoped in their demo code, which brings me to the last thing I don’t understand. The code looks like;

local json = require "json" if launchArgs and launchArgs.notification then native.showAlert( "launchArgs", json.encode( launchArgs.notification ), { "OK" } ) end -- notification listener local function onNotification( event ) if event.type == "remoteRegistration" then local PushToken = event.token local PW\_APPLICATION = "006F9-EE7C8" local PW\_URL = "https://cp.pushwoosh.com/json/1.3/registerDevice" local deviceType = 1 if ( system.getInfo("platformName") == "Android" ) then deviceType = 3 end local function networkListener( event ) if ( event.isError ) then native.showAlert( "Notification Registration Failed", "An Error Contacting the Server has Occurred. Please try again later from the application settings.", { "OK" } ) else end end local commands\_json = { ["request"] = { ["application"] = PW\_APPLICATION, ["push\_token"] = PushToken, ["language"] = system.getPreference("ui", "language"), ["hwid"] = system.getInfo("deviceID"), ["timezone"] = 3600, ["device\_type"] = deviceType } } local jsonvar = {} jsonvar = json.encode(commands\_json) local post = jsonvar local headers = {} headers["Content-Type"] = "application/json" headers["Accept-Language"] = "en-US" local params = {} params.headers = headers params.body = post network.request ( PW\_URL, "POST", networkListener, params ) elseif event.type == "remote" then native.showAlert( "remote", json.encode( event ), { "OK" } ) end end Runtime:addEventListener( "notification", onNotification )

So I’m now getting a launchArg alert, which is great, but it comes with a load of JSON encoding that I can’t caorrelate to any part of my code, so I guess the json.encode call brings this ‘extra’ data with it… The alert looks like this:

{“badge”:3,“type”:“remote”,“name”:“notification”,“custom”:“sound”:“default”,“alert”:“this is my actual puch notification”,“applicationState”:“inactive”}

Obviously, I just want my notification, not the spec’ data, that I can only assume comes from ‘local headers’ (guessing)… Any idea how I can just extrapolate the push and ommit this status-data? It’s all Chinese to me.

Thanks

launchArgs is a Lua table that has some things in it.  The thing you are interested in with a push notifications is it’s member launchArgs.notification, which is another Lua table.  The contents of that notification table includes:

launchArgs.notification.badge  – a number

launchArgs.notification.type – a sting that will always be “remote”

launchArgs.notification.name – a string that will be “notification”

launchArgs.notification.alert – a string with the text message sent along with the push

launchArgs.notification.applicationState – a string that lets you know the state of your app when the notification was received.  In your example the value is “inactive” meaning the app was code started.  Since this is part of launchArgs, it will always be “inactive” if the app was code started.

launchArgs.notification.custom – a table of custom data.  In this case it has a single member:   sound = “default”.

The json.encode() there is a simple way of taking a Lua table and turning it into a single string that can be easily printed (or in this case with native.showAlert()).  The example isn’t doing anything with JSON other than making the table printable.

If you read the documentation for the notification event that is triggered through the event handler, you will notice that those members (badge, type, name, alert, applicationState, custom) are all the members of the event table passed to the notification event.

In your event handler you have another native.showAlert() that’s taking the event table and turning into a printable string using json.encode.  If it helps you instead of doing the native.showAlert that you have, you could do:

native.showAlert(event.alert, {“Okay”})

To help you with the output from JSON to make it something you might understand a little better.  Simply put it so that there is a line break where there are commas:

{“badge”:3,“type”:“remote”,“name”:“notification”,“custom”:“sound”:“default”,“alert”:“this is my actual puch notification”,“applicationState”:“inactive”}

becomes

{

  “badge”:3,

  “type”:“remote”,

  “name”:“notification”,

  “custom”:“sound”:“default”,

  “alert”:“this is my actual puch notification”,

  “applicationState”:“inactive”

}

Now take the : and put in an =

{

  “badge” = 3,

  “type” = “remote”,

  “name” = “notification”,

  “custom” = “sound”:“default”,

  “alert” = “this is my actual puch notification”,

  “applicationState” = “inactive”

}

It’s starting to look more like a Lua table right?  Now we just have to take the quotes off the key and assign it to a table.

event =

{

  badge = 3,

  type = “remote”,

  name = “notification”,

  custom = “sound”:“default”,

  alert = “this is my actual puch notification”,

  applicationState = “inactive”

}

Now it is a Lua table.  JSON isn’t that hard to read.

Rob

Dear All, 

I have started using PushWhoosh with Corona and more or less have it working. 

I am getting push notifications and

  1. if the app is open we get event.type == “remote” event
  2. if the app is not open, we get a tray notification and on clicking it the app starts and we get a handle on the notification contents again (as above)

I do have 1 scenario which is a bit puzzling. Say there is a push notification that has arrived (the app is not open), now if the user does not view the notification but instead opens the app directly, I am unable to get the message which the notification has sent. 

Any pointers on this will be much appreciated.

Cheers,

Anshuman

Anshuman, unfortunately that’s the way push notifications work.  If you invoke the app on it’s own, it has no way of knowing about the push.

Rob

Thanks for your reply Rob. Apparently if I clear off the notifications from the tray, and then open the App, even then the app doesnt get those notifications. 

This has thrown a spanner in the works, as now the chat is no longer “reliable”, and possibly the recipient will miss the chat unless they have the app already open or click on the notification to open the app.

Any suggestions on how to implement a resilient chat system would be much appreciated!

Cheers,

Anshuman 

You should use some type of on line server that actually take care of receiving messages.  When the user connects, it can fetch the last X messages and compare that with messages it’s received and go that way.  Push is not really a good way to do chat.  Push is more of a way to remind people who are not in your app to get a reminder to them to do something.  Clearing the push list from the OS never activates the app.  Launching the app from the icon also doesn’t communicate with the push.

Thanks Rob, I have now gone down that route. Have written a nodejs powered chat API for ‘chat sync’ whenever the user opens the apps (or on App Resume event), which syncs any new chats on server to local app DB. Live chats are still via push notifications exchange, but will consider using socket connections or pub-sub maybe. 

Cheers