Notifications! How can I send data with my notifications and receive it in Lua within my Corona App?

I don’t have a paid PushWoosh account so I can’t look at their custom data format. If you have an account that can look at that form, perhaps a screen shot would help.

Rob

okay. I have two screen shots of the Corona docs from PushWoosh. But how can I add these images here in this Forum? When I drag either a JPG or PNG into this window, it doesn’t accept the image type. When I click on the image button, it asks me for the URL. When I click My Media, I can not find where to upload the files. And I’ve found no mention of how to do this in the forum documentation. Your continued help is much appreciated.

Click on the “More Reply Options” button at the bottom. You can upload images there.

Rob

Unbelievable! Your forum system gave me “Upload Failed” for JPG, PNG and ZIP files. I’d send you a screen shot, but I can’t. So, I’m sending you the screen shot of the Corona Forum Upload Error and the PushWoosh errors by posting it on my website. Please let me know when you have it so that I can remove it from my site.

www.multimediabible.com/PushWooshCorona.zip

Seriously, it shouldn’t be this difficult to post an image.

test

I’m not sure why you can’t upload them.

Rob

Your images are of the code to support push. I was wanting to the see the screens where you actually entered your push notification to send. There is a place to enter “custom” data. That’s what I’m looking for.

Here you go: www.multimediabible.com/PushWooshCorona2.zip - there is no information anywhere on how to use this.

Try something like:

{ “key1” : “value1”, “key2” : “value2” }

Then in your notification event should have a table called event.custom and it should hold that block of JSON data that you can json.decode() into a Lua table you can use.

Rob

Here is my current code - please be more clear about how to capture the data:

function NT.notificationListener( event )
    if V.notificationsOkay~=0 then
        print( “Notification event.type:”…event.type )
        print( “Notification event.name:”…event.name )    
        if ( event.type == “remote” or event.type == “local” ) then
            --handle the push or local notification
            print(“Notification is REMOVE or LOCAL”)
            print(“event.token:”…tostring(event.token))
            print(“event.text:”…tostring(event.text))
        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”, event.token )
            local PW_APPLICATION = “xxxxx-xxxxx”  --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”, V.deviceID)  --system.getInfo(“deviceID”) )
                end
            end    
            
            local commands_json =
            {
                [“request”] = {
                    [“application”] = PW_APPLICATION,
                    [“push_token”] = deviceToken,
                    [“language”] = V.chosenAbbrev,  --system.getPreference( “ui”, “language” ),  --“en”
                    [“hwid”] = V.deviceID,  --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
    else
        if ( event.type == “remoteRegistration” ) then
            V.notificationEvent=event
        end
    end
end

Hi Rob, on a similar but separate not, to setup a local notification, this is my code:

    local options = {
        alert = “Come Back to myGame”,
        badge = 2,
        sound = “alarm.caf”,
        custom = { foo = “bar” }
    }
    local newID = notifications.scheduleNotification( 60, options )  --604800 seconds = 1 week

You had mentioned there is a way to remove all notifications when I load up my iOS App? Can you please share that with me here, too?

Please in the future use code formatting for posting code. It’s the blue <> button

function NT.notificationListener( event ) if V.notificationsOkay~=0 then print( "Notification event.type:"..event.type ) print( "Notification event.name:"..event.name ) if ( event.type == "remote" or event.type == "local" ) then --handle the push or local notification print("Notification is REMOVE or LOCAL") print("event.token:"..tostring(event.token)) print("event.text:"..tostring(event.text)) print( json.prettify( event ) ) -- dump the event table 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", event.token ) local PW\_APPLICATION = "xxxxx-xxxxx" --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", V.deviceID) --system.getInfo("deviceID") ) end end local commands\_json = { ["request"] = { ["application"] = PW\_APPLICATION, ["push\_token"] = deviceToken, ["language"] = V.chosenAbbrev, --system.getPreference( "ui", "language" ), --"en" ["hwid"] = V.deviceID, --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 else if ( event.type == "remoteRegistration" ) then V.notificationEvent=event end end end

The only code I made to your code was adding a print to dump the event table so you can see what you’re getting.

In the PushWoosh interface in that custom field type what I suggested before and send your push notification from PushWoosh.

Rob

On iOS, when the app starts or comes back into the foreground, any notifications you have received will be cleared. Any pending local notifications as long as you have not exited your app can still be canceled with the cancel function. The only thing where it becomes a problem is if you completely exit your app you loose the handles to the notifications and you can’t do much about them.

Rob

Hi Rob, you wrote: “On iOS, when the app starts or comes back into the foreground, any notifications you have received will be cleared.”

  1. Am I to assume that my App will receive the notification events (via the listener code) once the App is loaded (if it wasn’t previously loaded)?
  2. Will this clearing affect all REMOTE and LOCAL notifications or just REMOTE?

You also wrote: “Any pending local notifications as long as you have not exited your app can still be canceled with the cancel function. The only thing where it becomes a problem is if you completely exit your app you loose the handles to the notifications and you can’t do much about them.”
3) I have noticed that even if I save the ID for a local notification, the next time the app is loaded Corona is unable to connect the saved local notification ID to the currently still-standing local notifications - so cancelling them later is impossible. Will a future Corona build resolve this?
4) For clarification, if I set a local notification and for any reason a user leaves the app, those local notifications will remain in place and there is no way for my app to turn them off? (This will undoubtedly frustrate any user - making local notifications essentially unusable in Corona). Do I understand this correctly?

I am about to test the new PushWoosh custom and report back my findings.

I have some GOOD and some BAD news - I’ll start with the good news. When the App is running, it gets the notification as you suggested. The one line of Lua code “print( json.prettify( event ) )” in the notification listener worked and shows the results:
{ “alert”:“This is a test message.”,  “applicationState”:“active”,  “custom”:{ “key1”:“test1”, “pw”:{ “p”:“4” },“key2”:“test2” }, “name”:“notification”, “sound”:“default”, “type”:“remote” }

The bad news is that when the App is NOT running, and I receive the notificiation, whether I click on the notification to bring up my App or load the App later, it doesen’t matter - in both cases, once the App loads up, the notification listener never receives the notification data. In other words, the ONLY way that I can “receive data” from a notification in a Corona App is if the App is actually running at the time the Push is sent.

If this cannot be resolved, then there is no way to send virtual goods to a gamer (i.e. holiday event, birthday, etc…) like King (makers of Candy Crush) - the need for a reliable notification is a vitally important function of any competitive App or platform (i.e. Corona). Are there any other things we can try to make it work? The only solution presently around this would be for me to use notification for the sole purpose of displaying a message to the user.

It’s also worth mentioning that because Apple (iOS) doesn’t allow us to keep the UDID anymore, every fresh install of our Apps shows up as another Device in the Push Woosh control panel. This is likely true for any notification service for iOS Apps.

But there is also another problem. Even after loading up my App, which no longer pushes any LOCAL notifications, the original notification shows up as (2) on my App icon from my iOS home page. So, is there no way to clear all outstanding notifications for our Apps, even if we can’t keep/store and use a notification ID to cancel it at a later date? What’s odd about this is that I completely deleted my App before reinstalling it. And even though PushWoosh recognizes my device as a new device because of the fresh install, it seems that iOS associates any previously outstanding local notifications with my freshly installed App - even though they are not received by my listener code. In other words, my App is stuck with a (2) on my App icon forever in such cases.

So, is there no way to clear all outstanding notifications for our Apps, even if we can’t keep/store and use a notification ID to cancel it at a later date?

Troy, you have a lot of questions and this thread is being difficult to follow and answer. Let me try to respond to all of these.

If your app is active, that is it’s in the foreground. You will get a notification event and your notification event listener will trigger.

If your app is backgrounded or completely stopped, the notification will come into the OS’s Notification system. You have to interact with that notification and your app will either start up from scratch or it will be foregrounded from the background. Depending on which state your app was in, you will get the notification in a couple of different ways. If you were backgrounded, you should get a notification event just like if you were active when the notification came in. If you’re code starting, you will get the push via launchArgs. There won’t be an event in this case.

UDID? There is a plugin OpenUDID that should take care of this for you.

The red 2 is called a badge. It’s actually not tied to any pending notifications. The sending push can request the badge number to be increased. There is an API Call native.setProperty() https://docs.coronalabs.com/api/library/native/setProperty.html that you can use to control the value of the badge.

Rob

I’m going to have to go to the pushNotifications sample app to get it for you or Google “corona launchArgs” to find tutorials for this. Give me a bit, I’m trying to finish up other stuff.

local launchArgs = ... print("### --- Launch Arguments ---") print( json.prettify( launchArgs ) )

The launchArgs table will contain the same values the event table does in a notification event.

I get an error using the term “…”