【发布时间】:2020-06-10 09:21:36
【问题描述】:
这是我的 DeviceNotification 命令
<?php
namespace App\Console\Commands;
use App\Notifications\WeatherNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
use Zttp\Zttp;
use Notification;
class DeviceNotification extends Command
{
protected $signature = 'Device:Notification';
protected $description = 'Making notification everyday with per device(User) with weather APi';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$allusers = DB::table('users')->where('account_type', 'farm_manager')->get();
$apiKey = 'cde1hjdsgfhbssb051njidsn65c90';
foreach ($allusers as $userone) {
$Lat = DB::table('users')->where('user_id', $userone->user_id)->value('x_loc');
$Lat = number_format($Lat, 4, '.', ',');
$Long = DB::table('users')->where('user_id', $userone->user_id)->value('y_loc');
$Long = number_format($Long, 4, '.', ',');
$response = Zttp::get("https://api.darksky.net/forecast/$apiKey/$Lat,$Long?units=ca");
$data = $response->json();
$rainprediction = 100 * $data['daily']['data']['0']['precipProbability'];
$tomoprediction = 100 * $data['daily']['data']['1']['precipProbability'];
$dayafteroprediction = 100 * $data['daily']['data']['2']['precipProbability'];
if ($rainprediction > 70.0) {
$msg = "There is " . $rainprediction . "% probability of rainfall today.";
} elseif ($rainprediction > 50.0 && $rainprediction < 70.0) {
$msg = "There is " . $rainprediction . "% probability of rainfall today.";
} else {
$msg = "There is " . $rainprediction . "% probability of rainfall today.";
}
if ($tomoprediction > 70.0) {
$msgtomoprediction = "There is " . $tomoprediction . "% probability of Tmorrow.";
} elseif ($tomoprediction > 50.0 && $tomoprediction < 70.0) {
$msgtomoprediction = "There is " . $tomoprediction . "% probability of Tomorrow.";
} else {
$msgtomoprediction = "There is " . $tomoprediction . "% probability of Tomorrow.";
}
if ($dayafteroprediction > 70.0)
{
$msgdayafteroprediction = "There is " . $dayafteroprediction . "% After Tomorrow.";
} elseif ($dayafteroprediction > 50.0 && $dayafteroprediction < 70.0) {
$msgdayafteroprediction = "There is " . $dayafteroprediction . "% probability Tomorrow.";
} else {
$msgdayafteroprediction = "There is " . $dayafteroprediction . "% Tomorrow.";
}
$user_id = $userone->farm_id;
$message_notification = array(
'today' => $msg,
'Tomorrow' => $msgtomoprediction,
'Day-After' => $msgdayafteroprediction,
);
//$userone->notify(new WeatherNotification($user_id, $message_notification));
Notification::send($userone, new WeatherNotification($user_id, $message_notification));
}
}
}
这是我的通知
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\User;
class WeatherNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
protected $user_id;
protected $message_notification;
public function __construct($user_id, $message_notification)
{
//
$this->user_id = $user_id;
$this->message_notification = $message_notification;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
//
'farm_id' => $this->user_id,
'weather_notification' => $this->message_notification,
];
}
}
我得到
Symfony\Component\Debug\Exception\FatalThrowableError : 调用未定义的方法 stdClass::notify()
当我使用 $userone->notify(new WeatherNotification($user_id, $message_notification));
和
这个: Symfony\Component\Debug\Exception\FatalThrowableError : 调用未定义的方法 stdClass::routeNotificationFor()
当我使用此 Notification::send($userone, new WeatherNotification($user_id, $message_notification)); 运行它时;
【问题讨论】:
标签: php laravel notifications task