[Resolved] Push notification and easyapns

Hello, I’m trying to get push notifications working with easyapns but having trouble. For some reason it doesn’t seem to be connecting to the server. If I use the address directly in a browser everything works fine, hoping someone can spot an obvious mistake I’ve made! Here’s the code I’m using:

local launchArgs = ...  
  
local json = require "json"  
  
if launchArgs and launchArgs.notification then  
 native.showAlert( "launchArgs", json.encode( launchArgs.notification ), { "OK" } )  
end  
  
local function networkListener( event )  
 if ( event.isError ) then --error occurred notify user  
 native.showAlert( "Notification Registration Failed", "Error", { "OK" } )  
 else  
 native.showAlert( "RESPONSE: " .. event.response )  
 end  
 end   
-- notification listener  
local function onNotification( event )  
 if event.type == "remoteRegistration" then  
 local easyurl = "http://clients.zerofiftyone.co.uk/push/apns.php?task=register"   
  
 easyurl=easyurl.."&appname="..\_G.appname  
 easyurl=easyurl.."&appversion="..\_G.appversion  
 easyurl=easyurl.."&deviceuid="..system.getInfo("deviceID")  
 easyurl=easyurl.."&devicetoken="..event.token  
 easyurl=easyurl.."&devicename="..system.getInfo("name")  
 easyurl=easyurl.."&devicemodel="..system.getInfo("model")  
 easyurl=easyurl.."&deviceversion="..system.getInfo("platformVersion")  
 easyurl=easyurl.."&pushbadge=enable"  
 easyurl=easyurl.."&pushalert=enable"  
 easyurl=easyurl.."&pushsound=enable"  
  
 easyurl=string.gsub(easyurl," ","%%20")  
  
 network.request ( easyurl, "GET", networkListener )  
 elseif event.type == "remote" then  
 native.showAlert( "remote", json.encode( event ), { "OK" } )  
 end  
end  
  
Runtime:addEventListener( "notification", onNotification )  

I’m getting the pop up on the phone that say’s error. also while I’m here, I’m unsure of the pushbadge, pushalert and pushsound settings and how you should set them properly, at the moment I’m just setting them all to enabled… [import]uid: 110859 topic_id: 30556 reply_id: 330556[/import]

I would make sure you are urlencoding all of those values. [import]uid: 19626 topic_id: 30556 reply_id: 122444[/import]

Thanks, encoding seemed to do the trick. I added headers in there too but doubt they made any difference in this case. I used your encode function from another thread :slight_smile: Here’s the code if anyone else searches:

[code]
local launchArgs = …

local json = require “json”

local function urlencode(str)
if (str) then
str = string.gsub (str, “\n”, “\r\n”)
str = string.gsub (str, “([^%w])”,
function © return string.format ("%%%02X", string.byte©) end)
str = string.gsub (str, " ", “+”)
end
return str
end

if launchArgs and launchArgs.notification then
native.showAlert( “launchArgs”, json.encode( launchArgs.notification ), { “OK” } )
end

local function networkListener( event )
if ( event.isError ) then --error occurred notify user
native.showAlert( “Notification Registration Failed”, “Error”, { “OK” } )
native.showAlert( “Notification Registration Failed”, event.url, { “OK” } )
else

end
end
– notification listener
local function onNotification( event )
if event.type == “remoteRegistration” then
local easyurl = “http://clients.zerofiftyone.co.uk/push/apns.php?task=register

easyurl=easyurl…"&appname="…_G.appname
easyurl=easyurl…"&appversion="…_G.appversion
easyurl=easyurl…"&deviceuid="…urlencode(system.getInfo(“deviceID”))
easyurl=easyurl…"&devicetoken="…event.token
easyurl=easyurl…"&devicename="…urlencode(system.getInfo(“name”))
easyurl=easyurl…"&devicemodel="…urlencode(system.getInfo(“model”))
easyurl=easyurl…"&deviceversion="…urlencode(system.getInfo(“platformVersion”))
easyurl=easyurl…"&pushbadge=enabled"
easyurl=easyurl…"&pushalert=enabled"
easyurl=easyurl…"&pushsound=enabled"

local headers = {}

headers[“Content-Type”] = “application/json”
headers[“Accept-Language”] = “en-US”

local params = {}
params.headers = headers

network.request ( easyurl, “GET”, networkListener, params )
elseif event.type == “remote” then
native.showAlert( “remote”, json.encode( event ), { “OK” } )
end
end

Runtime:addEventListener( “notification”, onNotification )
[/code] [import]uid: 110859 topic_id: 30556 reply_id: 122456[/import]

I would make sure you are urlencoding all of those values. [import]uid: 19626 topic_id: 30556 reply_id: 122444[/import]

Thanks, encoding seemed to do the trick. I added headers in there too but doubt they made any difference in this case. I used your encode function from another thread :slight_smile: Here’s the code if anyone else searches:

[code]
local launchArgs = …

local json = require “json”

local function urlencode(str)
if (str) then
str = string.gsub (str, “\n”, “\r\n”)
str = string.gsub (str, “([^%w])”,
function © return string.format ("%%%02X", string.byte©) end)
str = string.gsub (str, " ", “+”)
end
return str
end

if launchArgs and launchArgs.notification then
native.showAlert( “launchArgs”, json.encode( launchArgs.notification ), { “OK” } )
end

local function networkListener( event )
if ( event.isError ) then --error occurred notify user
native.showAlert( “Notification Registration Failed”, “Error”, { “OK” } )
native.showAlert( “Notification Registration Failed”, event.url, { “OK” } )
else

end
end
– notification listener
local function onNotification( event )
if event.type == “remoteRegistration” then
local easyurl = “http://clients.zerofiftyone.co.uk/push/apns.php?task=register

easyurl=easyurl…"&appname="…_G.appname
easyurl=easyurl…"&appversion="…_G.appversion
easyurl=easyurl…"&deviceuid="…urlencode(system.getInfo(“deviceID”))
easyurl=easyurl…"&devicetoken="…event.token
easyurl=easyurl…"&devicename="…urlencode(system.getInfo(“name”))
easyurl=easyurl…"&devicemodel="…urlencode(system.getInfo(“model”))
easyurl=easyurl…"&deviceversion="…urlencode(system.getInfo(“platformVersion”))
easyurl=easyurl…"&pushbadge=enabled"
easyurl=easyurl…"&pushalert=enabled"
easyurl=easyurl…"&pushsound=enabled"

local headers = {}

headers[“Content-Type”] = “application/json”
headers[“Accept-Language”] = “en-US”

local params = {}
params.headers = headers

network.request ( easyurl, “GET”, networkListener, params )
elseif event.type == “remote” then
native.showAlert( “remote”, json.encode( event ), { “OK” } )
end
end

Runtime:addEventListener( “notification”, onNotification )
[/code] [import]uid: 110859 topic_id: 30556 reply_id: 122456[/import]