I am not able to send custom data to iOS via push notifications and it’s not clear why.
Since I am very new to corona, I assume this is my error. However, I don’t know how to resolve.
I previously implemented urbanairship as my provider for push, as they are mentioned on the corona docs as a provider. However, after google turned up several people also with this exact issue - and no resolution, I switched providers to PushWoosh, hoping it would resolve the issue.
I am attempting to send a message from python (via rest/POST) to pushwoosh. While notifications are delivered, custom data is not sent to the device. Or, if it is sent to the device, it’s not appearing in the onNotification event. Or, some other error is occurring.
- Implemented pushwoosh
- able to receive push notifications on the device
- custom data always empty.
-
Updated Config.lua to include “custom” even though this is never explicitly mentioned in the docs.
notification = { iphone = { types = { “badge”, “sound”, “alert”, “custom” } } },
Read and reviewed teh following posts on this specific subject; none of which resolve the issue.
- https://developer.coronalabs.com/forum/2013/02/14/sending-push-custom-data
- http://forums.coronalabs.com/topic/29982-custom-push-data/?hl=%2Bcustom+%2Bdata+%2Bnotifications
- http://forums.coronalabs.com/topic/43605-bug-ios-push-messages-custom-data-parsing/?hl=payload#entry241249
- http://docs.coronalabs.com/guide/events/appNotification/index.html
- http://forums.coronalabs.com/topic/46603-push-crashes-ios-app-with-incorrect-custom-payload/?hl=%2Bcustom+%2Bdata#entry242335
- http://forums.coronalabs.com/topic/33961-ios-push-notification-custom-data-a-wrong-solution/
Custom data is always returned as an empty array “[]” instead of an empty object to the device. Though, preferably, it wouldn’t be empty at all. Which, as noted in other posts, appears to be an “upstream” conversion as Perry said in the aforementioned posts.
Payload returned to device as json.encode(event).
{"type":"remote","name":"notification","custom":[],"sound":"default","alert":"We all love notifications","applicationState":"active"} null
The pushwoosh documentation says it will return custom data as the"u" parameter. But there is no such parameter in the event object.
// JSON string or JSON object, will be passed as “u” parameter in the payload
Python JSON data to construct request. Out of desperation, I have “peppered” the data going to pushwoosh with some extra custom data, hoping something might be delivered.
data = { "request":{ "application":app\_code, # "applications\_group":"GROUP\_CODE", "auth":api\_access\_token, "notifications":[{ "send\_date":"now", "content":msg, "ios\_root\_params":{ "extra":{"motherfoo":"bar"}, "aps":{ "content-available": "1" } }, "data":{ # // JSON string or JSON object, will be passed as "u" parameter in the payload "custom": {"foo":True, "what":"lol"}, "extra": {"foo2extra":True, "what":"lol"} }, "custom": {"topfoo":True}, "platforms": [1], "devices":[device\_token], }] } }
Current Lua / Corona SDK code (Essentially the example code on pushwoosh)
local function onNotification( event ) print("onNotification()", json.encode(event), json.encode(event.u)) if event.type == "remoteRegistration" then local device\_token = event.token local PW\_APPLICATION = "MY APP TOKEN" -- use your app id in pushwoosh local PW\_URL = "https://cp.pushwoosh.com/json/1.3/registerDevice" local deviceType = 1 --// default to iOS if ( system.getInfo("platformName") == "Android" ) then deviceType = 3 end local function networkListener( event ) if ( event.isError ) then --// error occurred notify user native.showAlert( "Notification Registration Failed", "An Error Contacting the Server has Occurred. " .. "Please try again later from the application settings.", { "OK" } ) else --// Registration worked perform any action you like here end end local commands\_json = { ["request"] = { ["application"] = PW\_APPLICATION, ["push\_token"] = device\_token, ["language"] = system.getPreference("ui", "language"), ["hwid"] = system.getInfo("deviceID"), ["timezone"] = 3600, -- // offset in seconds ["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 print("Custom data is ", event.custom, json.encode(event.custom)) native.showAlert( "My Game", event.alert, {"OK"} ) end end Runtime:addEventListener( "notification", onNotification )
Corona version info
Basic 2014.2189
Note: the Corona SDK docs for implementing push point to a full reference, which doesn’t actually mention anything about notifications.
In addition, you must include a notification table in the config.lua file (reference) to enable various features for push notifications. This tells the system which of the alert types you plan to use.
- doc: http://docs.coronalabs.com/guide/events/appNotification/index.html
- reference: http://docs.coronalabs.com/guide/basics/configSettings/index.html
Additional reference materials
http://docs.coronalabs.com/guide/distribution/iOSBuild/index.html