Saturday , January 15 2022

Create Chat App for Android using GCM Part 1 – Android Coding by DMTechnolab

<? php

require once ‘../include/DbOperation.php’;

require once ‘../libs/gcm/gcm.php’;

the wanted ‘.././libs/Slim/Slim.php’;

_thin _thin::Registerautolader());

$ App = New _thin _thin());

// user id from db – global variable

$ user_id = Invalid;

/ **

* User Registration

* url – / register

* Method – Post

* Param – name, email

* /

$ App->Post()‘/ Register’, The ceremony ()) Use ()$ App) {{

// validating parameters

Verified()Array()‘Name’, ‘E-mail’));

// response array

$ Response = Array());

// receiving request parameters

$ Name = $ App->Request->Post()‘Name’);

$ Email = $ App->Request->Post()‘E-mail’);

// Wading Email

Validate email()$ Email);

// create a db object

$ Db = New DbOperation());

// Including user in database

$ Race = $ Db->create user()$ Name, $ Email);

// if user created

// add user details to react

If ()$ Race == USER_CREATED_SUCCESSFULLY) {{

$ Response[“error”] = false;

$ User = $ Db->GetUser()$ Email);

$ Response[‘id’] = $ User[‘id’];

$ Response[‘name’] = $ User[‘name’];

$ Response[’email’] = $ User[’email’];

Echo()201, $ Response);

// If creating a user fails to add an error to the response

} other If ()$ Race == USER_CREATE_FAILED) {{

$ Response[“error”] = True;

$ Response[“message”] = “Oops! An error occurred while registering”;

Echo()200 rupees, $ Response);

// if user already exists

// adding user data in response

} other If ()$ Race == USER_ALREADY_EXISTED) {{

$ Response[“error”] = false;

$ User = $ Db->GetUser()$ Email);

$ Response[‘id’] = $ User[‘id’];

$ Response[‘name’] = $ User[‘name’];

$ Response[’email’] = $ User[’email’];

Echo()200 rupees, $ Response);

}

});

/ *

* URL: / send

* Method: Post

* Parameter: ID, Message

* * /

// It is used to send messages to chat rooms

$ App->Post()‘/ Send’, The ceremony ()) Use ()$ App) {{

// Verification of required parameters

Verified()Array(), ‘message’));

// receiving request parameters

$ Id = $ App->Request())->Post());

$ Message = $ App->Request())->Post()‘message’);

$ Name = $ App->Request())->Post()‘Name’);

// create a gcm object

$ Gcm = New GCM());

// create a db object

$ Db = New DbOperation());

// Creating a response array

$ Response = Array());

// create an array containing message data

$ pushdata = Array());

// adding title that will be the username

$ pushdata[‘title’] = $ Name;

// add a message to be sent

$ pushdata[‘message’] = $ Message;

// Add a user ID to identify the user sending the message

$ pushdata[‘id’]=$ Id;

// If the message is successfully added to the database

If ()$ Db->Add message()$ Id, $ Message)) {{

// Sending push notification with gcm object

$ Gcm->send Message()$ Db->getRegistrationTokens()$ Id), $ pushdata);

$ Response[‘error’] = false;

} other {{

$ Response[‘error’] = True;

}

Echo()200 rupees, $ Response);

});

/ *

* URL: / storegcmtoken /: id

* Method: PUT

* Parameter: Token

* * /

// This will store the gcm token for the database

$ App->Branch()‘/ storegcmtoken /: id’, The ceremony ()$ Id) Use ()$ App) {{

Verified()Array()‘Token’));

$ Token = $ App->Request())->Branch()‘Token’);

$ Db = New DbOperation());

$ Response = Array());

If ()$ Db->storeGCMToken()$ Id, $ Token)) {{

$ Response[‘error’] = false;

$ Response[‘message’] = “Stored token”;

} other {{

$ Response[‘error’] = True;

$ Response[‘message’] = “Token could not be stored”;

}

Echo()200 rupees, $ Response);

});

/ *

* URL: / message

* Method: G.E.T.

* * /

// This will fetch all the messages available on the database to display on the thread

$ App->get()‘/ message’, The ceremony ()) Use ()$ App){{

$ Db = New DbOperation());

$ Message = $ Db->getMessages());

$ Response = Array());

$ Response[‘error’]=false;

$ Response[‘messages’] = Array());

whereas()$ Row = mysqli_fetch_array()$ Message)){{

$ Tem = Array());

$ Temporary[‘id’]=$ Row[‘id’];

$ Temporary[‘message’]=$ Row[‘message’];

$ Tem[‘userid’]=$ Row[‘users_id’];

$ Temporary[‘sentat’]=$ Row[‘sentat’];

$ Temporary[‘name’]=$ Row[‘name’];

array_push()$ Response[‘messages’],$ Temporary);

}

Echo()200 rupees,$ Response);

});

// act of validating email

The ceremony Validate email()$ Email)

{{

$ App = _thin _thin::get Instance());

If ()!Filter_war()$ Email, FILTER_VALIDATE_EMAIL)) {{

$ Response[“error”] = True;

$ Response[“message”] = ‘Email address is not valid’;

Echo()400, $ Response);

$ App->Stop());

}

}

// act of displaying the response in the browser

The ceremony Echo()$ status_code, $ Response)

{{

$ App = _thin _thin::get Instance());

// Http Response Code

$ App->Event()$ status_code);

// Reply content type to Jason

$ App->content type()‘Application / json’);

Echo json_encode()$ Response);

}

// work to verify the required parameters

The ceremony Verified()$ Required_ field)

{{

$ Error = false;

$ error_fields = “”;

$ request_params = $ _REQUEST;

// handling PUT request params

If ()$ _SERVER[‘REQUEST_METHOD’] == ‘Branch’) {{

$ App = _thin _thin::get Instance());

parse_str()$ App->Request())->getBody()), $ request_params);

}

For each ()$ Required_ field As $ Field) {{

If ()!disturbed()$ request_params[$field]) || vagabond()Trim()$ request_params[$field])) <= 0) {{

$ Error = True;

$ error_fields .= $ Field . ;

}

}

If ()$ Error) {{

// required fields (s) are missing or empty

// close the echo error jones and app

$ Response = Array());

$ App = _thin _thin::get Instance());

$ Response[“error”] = True;

$ Response[“message”] = ‘Required Fields) ‘ . matter()$ error_fields, 0, 2) . ‘Missing or empty’;

Echo()400, $ Response);

$ App->Stop());

}

}

The ceremony Certify() _thin _way $ Route)

{{

// implement authentication when needed

}

$ App->Daud());


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

Your email address will not be published. Required fields are marked *