How can echo a json data after an email sent in codeigniter - android

I am using codeigniter email, to send an email to the client who has registerd him self into the system, and after that I echo a json code that syas an email has been sent to you.
this proccess is working when i am using web browser, but it is not working in android application.
my code is below:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.1and1.com',
'smtp_port' => 587,
'smtp_user' => $from_email,
'smtp_pass' => $this->config->item('email_password'),
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from($from_email, "something");
$this->email->reply_to($from_email, "something");
$this->email->to($to_email);
$this->email->subject("Account Verification");
$this->email->set_mailtype("html");
$this->email->message($email_body);
if($this->email->send()){
$rsp['error'] = 0;
$rsp['data'] = $user;
echo json_encode($rsp);}else{do some thing...}
but if i remove the $this->email->send() from code, then the echo json code works fine, but not emil sendig.

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.1and1.com',
'smtp_port' => 587,
'smtp_user' => $from_email,
'smtp_pass' => $this->config->item('email_password'),
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from($from_email, "something");
$this->email->reply_to($from_email, "something");
$this->email->to($to_email);
$this->email->subject("Account Verification");
$this->email->set_mailtype("html");
$this->email->message($email_body);
$this->email->send();
$rsp['error'] = 0;
$rsp['data'] = $user;
echo json_encode($rsp);
like this try once

Your code:
if ($this->email->send()) {
echo json_encode($rsp);
} else {
// do some thing...
}
means that JSON will be printed only if send() function return true. If it returns false, then the logic will go to else section and no JSON will be printed (unless you modify // do some thing to print JSON also).
I'm pretty sure that mail() return false. You are using CI, don't forget to check application/logs/*.log to see the error detail.

Related

FCM Config Problem Push Notif Android & iOs in Laravel

I got some problems with FCM in my projects.
This is my config FCM in .env
FCM_SERVER_KEY_ANDRO=thisKeyForAndroid
FCM_SERVER_KEY_IOS=thisKeyForIOS
FCM_SENDER_ID=senderId
And this is my config fcm.php
<?php
return [
'driver' => env('FCM_PROTOCOL', 'http'),
'log_enabled' => false,
'http' => [
'server_key' => env('FCM_SERVER_KEY_ANDRO'), //the problem is here
'sender_id' => env('FCM_SENDER_ID'),
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'timeout' => 30.0, // in second
],
];
I have a problem when I use FCM_SERVER_KEY_ANDRO for server_key, push notification in IOS is not working. But when I use FCM_SERVER_KEY_IOS for server_key, push notification in IOS is working but not in Android.
I can't for adding new parameter on http for key android or key ios.
I want FCM_SERVER_KEY_ANDRO and FCM_SERVER_KEY_IOS to run concurrently on fcm.php, but i don't know how to run both?
Sorry for my bad grammar
you can use config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); for android and config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); for iOS but you need to separate the function to make this work. you can put this code before $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
Here the example:
public function ios($data, $notifications, $token)
{
config(['fcm.http.server_key' => env('FCM_SERVER_KEY_IOS')]);
......
......
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
return response()->default(200, 'Sent', $downstreamResponse);
}
public function android($data, $notifications, $token)
{
config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]);
......
......
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
return response()->default(200, 'Sent', $downstreamResponse);
}

FCM Push notification not received on all devices

I have data notification and on some devices i always receive it,on some only when app is running and on one device i never receive it.
I set priority to high but nothing changes im not sure is it written proper, here is php code
$naslovPoruke = "Пробна апликација";
$path_to_fcm = "https://fcm.googleapis.com/fcm/send";
$server_key =my_key_here";
$key = "/topics/sve";
$msg = array
(
'title' => $naslovPoruke,
'body' => $poruka,
'idObjave' => $idObjave,
'naslovObjave' => $naslovObjave,
'tekstObjave' => $tekstObjave,
);
$headers = array
(
'Authorization:key=' .$server_key,
'Content-Type:application/json'
);
//$fields = array
//(
// 'to' => $key,
// 'notification' => array('title'=>$naslovPoruke,'body'=>$poruka),
// 'data' => $msg
//);
$fields = array
(
'to' => $key,
'data' => $msg,
'priority'=>"high",
'ttl'=>"0"
);
$payload = json_encode($fields);
echo $payload;
$curl_session = curl_init();
curl_setopt($curl_session,CURLOPT_URL,$path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST,true);
curl_setopt($curl_session,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,$headers);
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS,$payload);
$result = curl_exec($curl_session);
echo $result;
curl_close($curl_session);
?>
Also this is json response:
{
"to": "\/topics\/sve",
"data": {
"title": "pushuptitle",
"body": "pushupbody",
"idObjave": "linktoimage.jpg",
"naslovObjave": "apptitle",
"tekstObjave": "appbody"
},
"priority": "high",
"ttl": "0"}
{
"message_id": 6358638519123612486
}
Im not sure why i dont receive this message on all devices?
You can first check if you have notifications enabled on the device that doesn't get the notification.

Creating new plugin for webservice

I wrote this code to connect to a database, make some queries and send them by json like web api.
I want to create plugin that will work like a WEB API. Then upload it on WP modules on internet and people may install them on their sites to connect my android App. Anywhere.
This is my php code (include db_connect and sending json and respond to my Android App)
<?php
if(!isset($_REQUEST ['id']))
echo "id is not set";
if (isset ( $_REQUEST ['id'] )) {
$id = $_REQUEST ['id'];
$response = array ();
mysql_connect('localhost','db_admin','password') or die(mysql_error());
mysql_select_db('db_name') or die(mysql_error());
$sql= "select * from employee";
$run=mysql_query($sql);
if (mysql_num_rows ($run) > 0) {
while ($row= mysql_fetch_array($run)){
$product = array ();
$product ["id"] = $row [0];
$product ["name"] = $row [1];
$product ["salary"] = $row [2];
$response [] = $product;
}
echo json_encode ( $response );
}
}
?>
My manager wants me not to use the default Woocommerse WORDPRESS API,so I have to create new plugin. and want to know how can I convert it to standard module?
Is it possible at all ?
If I was you I will created like this:
First off would create handler for the request, either via ajax (im not sure if this way is supported in android ) like this;
function get_some_information(){
$user_id = $_REQUEST['id'];
if(!isset($user_id) || empty($user_id)){
return json_encode([
'success' => false,
'message' => 'Empty Request'
]);
}
$data = get_info_from_data($user_id);
if(!empty($data)){
echo json_encode($data);
exit;
}else{
echo json_encode([
'success' => false,
'message' => 'Error while processing data'
]);
exit;
}
}
add_action('wp_ajax_get_some_information', 'get_some_information'); //for auth users
add_action( 'wp_ajax_nopriv_get_some_information', '_get_some_information' ); //for the rest
Note: there is an exit; you always need to terminate the application to be able to get the json, otherwise the output it will be 0
With this now we have an access point to data and validation; and it looks a bit more clean.
function get_some_information($id){
$result = [];
// do all things and added to the array
return $result;
}

Facebook API through Android and login into Symfony2

I've got an app on Android which I got access to my profile, but with that data (may be access_token?) I'd like to do login into my Symfony2 project without any kind of password. Or should I do the login through Symfony2 and the interaction betwen the Android App and my Symfony2 project?
What should I do?
Anyway thanks for anyone for the tips.
You can use the graph API from facebook
From the android application, you get the access_token from the sdk.
And to get a token from your SF2 application, You call the /login/facebook?token=[access_token] api.
$url = 'https://graph.facebook.com/me?access_token='.$token;
$response = $this->wget($url);
$data = json_decode($response['content'], true);
if(isset($data['error'])){
// if the response has errors
throw new HttpException(self::ERROR_CODE, $data['error']['message']);
} else if(isset($data['verified']) && $data['verified'] == true){
// Your user is successfully authenticated, you can retrieve is personal information from $data
// What you get depend of your FB app permissions
} else {
throw new HttpException(self::ERROR_CODE, self::ERROR_INVALID_FACEBOOK_USER_MESSAGE);
}
wget util here:
private function wget($url)
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}

The push notification format of Pushwoosh and Phonegap

Now the pushwoosh works without problem. I can see the notification on the upper left corner of the Android phone. How can I set the notification to be pop-up onto screen rather than the notification on upper left only, even the phone is in silence mode!
Which way should I go for the customization? Thx
function pwCall( $action, $data = array() ) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $action;
$json = json_encode( array( 'request' => $data ) );
$res = doPostRequest( $url, $json, 'Content-Type: application/json' );
print_r( #json_decode( $res, true ) ); }
pwCall( 'createMessage', array( '
application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => array(
array(
'send_date' => 'now',
'content' => 'test',
'ios_badges' => 3,
'data' => array( 'custom' => 'json data' ),
'link' => 'http://pushwoosh.com/'
)
)
)
);
To configure how to display the incoming push notification, you should write your own logic in the receiver in Android app, as specified in this page.
Currently, the tutorial currently uses the following the show the message:
private void showMessage(String message)
{
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
Try to create your own logic (user interface, popup method & timing, etc) in the function.

Categories

Resources