Corona sdk (lua) how to send request to php script for registering a device to get notifications?

I am working on an app that requires to get push notifications

I couldn't get PushWoosh to work (device logged succesful, but there were no subscribers) so the people I'm working with opted for configuring their own server to send notifications to GCM.

The PHP Register code is as follows:

require_once('loader.php');

// return json response
$json = array();

$nameUser  = $_POST["name"];
$nameEmail = $_POST["email"];

// GCM Registration ID got from device
$gcmRegID  = $_POST["regId"];

/**
 * Registering a user device in database
 * Store reg id in users table
 */
if (isset($nameUser)
     && isset($nameEmail)
     && isset($gcmRegID)) {

    // Store user details in db
    $res = storeUser($nameUser, $nameEmail, $gcmRegID);

    $registatoin_ids = array($gcmRegID);
    $message = array("product" => "shirt");

    $result = send_push_notification($registatoin_ids, $message);

    echo $result;
} else {
    // user details not found
}

According to the example that we got the php code from, if I was writing in native code, I would need to do this:

void register(final Context context, String name, String email, final String regId) {
Log.i(Config.TAG, "registering device (regId = " + regId + ")");

String serverUrl = Config.YOUR_SERVER_URL;

Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("name", name);
params.put("email", email);

long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);

// Once GCM returns a registration id, we need to register on our server
// As the server might be down, we will retry it a couple
// times.
for (int i = 1; i <= MAX_ATTEMPTS; i++) {

    Log.d(Config.TAG, "Attempt #" + i + " to register");

    try {
        //Send Broadcast to Show message on screen
        displayMessageOnScreen(context, context.getString(
                R.string.server_registering, i, MAX_ATTEMPTS));

        // Post registration values to web server
        post(serverUrl, params);
         ..........
         ....

And I guess Corona's equivalent to post(serverUrl, params); would be network.request(serverURL,"POST",listener,params) Which I've tried to send several times in different ways, but I always get the same error from the server.

[Sun Jun 08 19:36:30 2014] [error] [client 181.55.xxx.xxx] PHP Notice:  Undefined index: name in /var/www/html/vhosts/vps44.econfe.com/htdocs/app/push/register.php on line 7
[Sun Jun 08 19:36:30 2014] [error] [client 181.55.xxx.xxx] PHP Notice:  Undefined index: email in /var/www/html/vhosts/vps44.econfe.com/htdocs/app/push/register.php on line 8
[Sun Jun 08 19:36:30 2014] [error] [client 181.55.xxx.xxx] PHP Notice:  Undefined index: regId in /var/www/html/vhosts/vps44.econfe.com/htdocs/app/push/register.php on line 11

My last attempt in Corona was this:

local commands_json  = {
            ["email"] = "test@test",
            ["name"] = system.getInfo("deviceID"),
            ["regId"] = DeviceID
        }

    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
    params.progress = "download"

    network.request ( SERVER_URL, "POST", networkListener, params )

This is the first time I'm using internet services, I've never sent or recieved anything before (or maybe just once, many years ago) So I could be overlooking something really simple, so any help or suggestion is highly appreciated.

I don't even know if I should be using this json thing, I've tried removing it (I don't know what to do with the header in that case) but the same thing happens, I'm using it because it was there in the Pushwoosh example for Corona (which, again, didn't work for me)

Answers


Well, after trying to send the information in many different formats I stumbled upon the solution. 1: I needn't use the json encoding. 2: the variables needed to be in just one string separated by the symbol '&'

local headers = {}
        headers["Content-Type"] ="application/x-www-form-urlencoded"
        headers["Accept-Language"] = "en-US"
        local body="name="..system.getInfo("deviceID").."&email=test@test&regId="..DeviceID
        local params = {}
        params.headers = headers
        params.body = body

        network.request ( SERVER_URL, "POST", networkListener, params )

Need Your Help

Implementing SIP for WebRTC on iOS

ios webrtc sip pjsip

I am building an RTC iOS app client. I am using the google WebRTC iOS library. However, since WebRTC doesn't implement signalling I am searching for an easy way to implement a SIP stack at the sign...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.