Error the Trying to get property 'android_id' of non-object - laravel

I want to send notification to android devices using laravel.I do not want use package,and I am using curl to send query .I am write this codes but it has error but it gets the error Trying to get property 'android_id' of non-object .
I am create help.php
function send_notification_FCM($android_id, $title, $message, $id,$type) {
$accesstoken = env('FCM_KEY');
$URL = 'https://fcm.googleapis.com/fcm/send';
$post_data = '{
"to" : "' . $android_id . '",
"data" : {
"body" : "",
"title" : "' . $title . '",
"type" : "' . $type . '",
"id" : "' . $id . '",
"message" : "' . $message . '",
},
"notification" : {
"body" : "' . $message . '",
"title" : "' . $title . '",
"type" : "' . $type . '",
"id" : "' . $id . '",
"message" : "' . $message . '",
"icon" : "new",
"sound" : "default"
},
}';
// print_r($post_data);die;
$crl = curl_init();
$headr = array();
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: ' . $accesstoken;
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($crl, CURLOPT_URL, $URL);
curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);
curl_setopt($crl, CURLOPT_POST, true);
curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
$rest = curl_exec($crl);
if ($rest === false) {
// throw new Exception('Curl error: ' . curl_error($crl));
//print_r('Curl error: ' . curl_error($crl));
$result_noti = 0;
} else {
$result_noti = 1;
}
//curl_close($crl);
//print_r($result_noti);die;
return $result_noti;
}
and in the controller :
public function notifyUser(Request $request){
$user = User::where('id', $request->id)->first();
$android_id = $user->android_id;
$title = "Greeting Notification";
$message = "Have good day!";
$id = $user->id;
$type = "basic";
$res = send_notification_FCM($android_id, $title, $message, $id,$type);
if($res == 1){
echo 'success';
// success code
}else{
// fail code
}
}
and my rout :
Route::get('firebase/notification', 'firebaseNotificationController#notifyUser');
my database:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('android_id')->nullable()->after('wallet');
});
}

$user = User::where('id', $request->id)->first();
the result of the first() method could be null, that because either of the wrong id or the request parameter is null, you should check it first:
$user = User::where('id', $request->id)->first();
if($user==null)
{
// fail finding user code
}
else
{
$android_id = $user->android_id;
$title = "Greeting Notification";
$message = "Have good day!";
$id = $user->id;
$type = "basic";
$res = send_notification_FCM($android_id, $title, $message, $id,$type);
if($res == 1){
echo 'success';
// success code
}else{
// fail code
}
}

Related

Automatically replace the first & with a ? question mark

I have this code below which generates a query string for my url.
Question I would like to know is there away that it could automatically replace the first & with a ? question mark no matter what the first $_GET is.
$url = '';
if ($this->input->get('directory')) {
$pos = strrpos($this->input->get('directory'), '/');
if ($pos) {
$url .= '&directory=' . urlencode(substr($this->input->get('directory'), 0, $pos));
}
}
if ($this->input->get('target')) {
$url .= '&target=' . $this->input->get('target');
}
if ($this->input->get('thumb')) {
$url .= '&thumb=' . $this->input->get('thumb');
}
$data['parent'] = base_url('image_manager' . $url);
Found solution
$find = '&';
$replace = '?';
$result = preg_replace("/$find/", $replace, $url, 1);
echo $result;

Magento 2 oauth 1 get request token

Am not able to get request token in magento app. Signature is always invalid.
This is the code i use to generate the signature. The consumer key is the one is i get from localhost:8000 (magento shop) request to localhost:3000.
I tried changing the url both to localhost 8000 and 3000.
Followed these instructions: Magento instructions
function getSignature($consumerKey)
{
$params = array(
'oauth_nonce' => uniqid(mt_rand(1, 1000)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
'oauth_consumer_key' => $consumerKey,
);
ksort($params);
$baseString = strtoupper('POST') . '&';
$baseString .= rawurlencode('http://localhost:8000') . '&';
$baseString .= rawurlencode(buildSignatureDataString($params));
$signature = hash_hmac('SHA1', $baseString, getSigningKey(), true);
return base64_encode($signature);
}
function getSigningKey()
{
return rawurlencode('magento_private_key') . '&';
}
function buildSignatureDataString(array $signatureData)
{
$signatureString = '';
$delimiter = '';
foreach ($signatureData as $key => $value) {
$signatureString .= $delimiter . $key . '=' . $value;
$delimiter = '&';
}
return $signatureString;
}

Laravel 5.2 orWhere generate "and" in query

I have written a query in Laravel 5.2 as below, where I used scope. The scope scopeSubscriberCriteriaSearch have further sub scope. The scenario is, if the main scope finds "first_name" "=" "test" OR "last_name" "!=" "demo" etc, the related sub scope will be called.
$record = self::leftJoin("$customersTable as customers", 'customers.email', '=', "{$this->table}.email")
->leftJoin("$campaignsTable as campaigns", 'campaigns.campaign_id', '=', "{$this->table}.campaign_id")
->select("$this->primaryKey", "{$this->table}.first_name", "{$this->table}.last_name", "{$this->table}.email", "{$this->table}.phone", "{$this->table}.address", "campaigns.campaign_id", "campaigns.campaign_name", "customers.order_count")
->whereRaw($where);
$record->subscriberCriteriaSearch($criteria, $search_type, $persist)
->searchAfter($waiting_min, $search_type, $this->user_id)
->excludeLeadEmail($last_used_email)
->greaterCreateDateSearch($campaign_start_date)
->get();
===========
The main scope is as below:
public function scopeSubscriberCriteriaSearch($query, $criteria, $search_type, $persist = TRUE)
{
if (!empty($criteria))
{
$criteria_var = ['first_name', 'last_name', 'email', 'phone_number', 'city', 'country', 'state', 'zip', 'address', 'campaign', 'affiliate', 'subAffiliate', 'createdAt'];
return $query->where(function($sub_query) use ($criteria, $criteria_var, $search_type, $persist)
{
foreach ($criteria as $rule)
{
if (in_array($rule->condition, $criteria_var))
{
$position = array_search($rule->condition, $criteria_var);
($rule->condition == 'affiliate' || $rule->condition == 'subAffiliate') ? $sub_query->{$criteria_var[$position] . 'Search'}($rule->logical, $rule->input_val, $search_type, $persist) : $sub_query->{$criteria_var[$position] . 'Search'}($rule->logical, $rule->input_val, $persist);
}
}
});
}
}
The sub scopes (for example:) as below:
public function scopeFirst_nameSearch($query, $operator, $first_name, $persist = TRUE)
{
$query_operator = $this->getQueryOperator($operator);
if (!empty($operator) && !empty($first_name))
{
$query_operator_val = preg_match('/like/i', $query_operator) ? ($operator == 'starts' ? '"' . $first_name . '%"' : ($operator == 'ends' ? '"%' . $first_name . '"' : '"%' . $first_name . '%"')) : '"' . $first_name . '"';
return $persist ? $query->where($this->table . ".first_name", $query_operator, DB::raw($query_operator_val)) : $query->orWhere($this->table . ".first_name", $query_operator, DB::raw($query_operator_val));
}
}
public function scopeLast_nameSearch($query, $operator, $last_name, $persist = TRUE)
{
$query_operator = $this->getQueryOperator($operator);
if (!empty($operator) && !empty($last_name))
{
$query_operator_val = preg_match('/like/i', $query_operator) ? ($operator == 'starts' ? '"' . $last_name . '%"' : ($operator == 'ends' ? '"%' . $last_name . '"' : '"%' . $last_name . '%"')) : '"' . $last_name . '"';
return $persist ? $query->where($this->table . ".last_name", $query_operator, DB::raw($query_operator_val)) : $query->orWhere($this->table . ".last_name", $query_operator, DB::raw($query_operator_val));
}
}
So, if $persist is False, then the orWhere part of these sub queries will execute. But in my case, the query now generated as below:
select prospect_id, sem_1_prospects.first_name, sem_1_prospects.last_name, sem_1_prospects.email, sem_1_prospects.phone, sem_1_prospects.address, sem_campaigns.campaign_id, sem_campaigns.campaign_name, sem_customers.order_count from sem_1_prospects left join sem_1_customers as sem_customers on sem_customers.email = sem_1_prospects.email left join sem_1_campaigns as sem_campaigns on sem_campaigns.campaign_id = sem_1_prospects.campaign_id where ((sem_1_prospects.first_name = "test") and (sem_1_prospects.last_name NOT LIKE "%demo%"));
Can someone help me to find out the issue?

Sitemap-XML in Processwire 3

how can i generate a sitemap for processwire 3 for huebert-webentwicklung.de/sitemap.xml it doesen't work with the Plugin MarkupSitemapXML. Any idea how to get it work?
Thanks.
Create a new page template (sitemap.xml) then set the page output to be XML the the PW backend. Create a page and link it (set it to hidden).
function renderSitemapPage(Page $page) {
return
"\n<url>" .
"\n\t<loc>" . $page->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" .
"\n</url>";
}
function renderSitemapChildren(Page $page) {
$out = '';
$newParents = new PageArray();
$children = $page->children;
foreach($children as $child) {
$out .= renderSitemapPage($child);
if($child->numChildren) $newParents->add($child);
else wire('pages')->uncache($child);
}
foreach($newParents as $newParent) {
$out .= renderSitemapChildren($newParent);
wire('pages')->uncache($newParent);
}
return $out;
}
function renderSitemapXML(array $paths = array()) {
$out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
array_unshift($paths, '/'); // prepend homepage
foreach($paths as $path) {
$page = wire('pages')->get($path);
if(!$page->id) continue;
$out .= renderSitemapPage($page);
if($page->numChildren) $out .= renderSitemapChildren($page);
}
$out .= "\n</urlset>";
return $out;
}
header("Content-Type: text/xml");
echo renderSitemapXML();

facebook app losing session values after redirection

I am using Facebook PHP SDK 3.1.1
This page is supposed to collect value from header and later use it for uploading images.
Problem: $_SESSION['file'] becomes null after user logs in to Facebook.
<?php
require 'facebook.php';
include('connect.php');
$facebook = new Facebook(array('appId' => "XXXXXX",'secret' => "XXXXX","cookie" => true,'fileUpload' => true));
session_start();
$user_id = $facebook->getUser();
if(isset($_REQUEST["src"]))
{
echo "<center><img src='loader.gif'/></center>";
echo "<center><div id='msg'>Exporting Image..</div></center>";
$_SESSION['file'] = $_REQUEST["src"];
$_SESSION['club'] = $_REQUEST["club"];
}
$code = $_REQUEST["code"];
if(empty($code))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=". $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
exit();
}
$token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url). "&client_secret=" . $app_secret . "&scope=user_photos,email,read_stream,publish_stream&code=".$code;
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$response = url_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token=". $params['access_token'];
$user = json_decode(url_get_contents('https://graph.facebook.com/me/albums?access_token='.$params['access_token']),true);
foreach($user as $key=>$value)
{
foreach($user[$key] as $key1=>$value1)
{
if($value1['name'] == "Photos")
{
$album_uid = $value1['id'];
}
}
}
if(!isset($album_uid))
{
echo 'Create an album';
$album_details = array(
'message'=> 'Uploaded via XXX',
'name'=> 'Photos'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'];
}
$photo_details = array('message'=>$_SESSION["club"]);
try{
$file = NULL;
$file="../Photos/".$_SESSION['file'].".jpeg";
echo '<script>document.getElementById("msg").innerHTML = "Uploading Image"</script>';
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
}catch(Exception $e){
echo "<script type='text/javascript'>top.location.href = 'http://www.facebook.com/PAGE_URL/';</script>";
exit();
}
$_SESSION['file'] is null
Am I doing it right ? or something is wrong with my web hosting ?
The problem is that your app is running inside of an IFRAME and the session cookies are not being preserved. Please see here:
Facebook Iframe App with multiple pages in Safari Session Variables not persisting

Resources