Laravel 5.7 使用 Expo 发送通知
Laravel 5.7 sending notifications using Expo
我正在为我的后端使用 Laravel 5.7(我是 Laravel 的新手)并且我正在尝试使用 Expo push notification extension for Laravel 向我的用户发送通知。
我按照说明的步骤进行操作,但我迷失了应该放置 class ExpoNotification extends Notification
的位置以及如何调用它。
我希望发生的是,每次订单状态发生变化时,都会向用户发送通知。
发生的事情是我收到一条错误消息,指出找不到 class
。
OrderController
public function update_order(Request $request, $id)
{
//Get the Order and update the status
Order::where('id', '=', $id )->update($request->only(['status']));
//Get the order with ::find so I can use $order->
$order = Order::find($id);
//Get user belonging to this order
$user= User::where('id', '=', $order->user_id);
//Get response with orders only posted the same day and are payed
$orders = Order::where('store_id', '=', $order->store_id)
->where('day', '=', $order->day )
->where('week', '=', $order->week )
->where('year', '=', $order->year )
->where('payment_status', '=', $order->payment_status)->get();
//send expo notification so the user gets his update
new ExpoNotification($order);
//return only relevant orders to the store
return OrderResource::collection($orders);
}
ExpoNotification
<?
namespace App\Notifications\ExpoNotification;
use App\Order;
use App\User;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
use Illuminate\Notifications\Notification;
class ExpoNotification extends Notification
{
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->body("Your {$notifiable->service} account was approved!");
}
}
邮递员出错
<!DOCTYPE html><!--
Symfony\Component\Debug\Exception\FatalThrowableError: Class 'App\Notifications\ExpoNotification' not found in file /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php on line 182
Stack trace:
1. Symfony\Component\Debug\Exception\FatalThrowableError->() /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php:182
回答
由穆罕默德提供
<?php
namespace App\Notifications;
use App\Order;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class ExNotification extends Notification
{
use Queueable;
protected $order;
public function __construct($order){
$this->order=$order;
}
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->body("Your {$notifiable->service} account was approved!");
}
public function toArray($notifiable)
{
return [
//
];
}
}
你的错误是你实现了你的 ExpoNotification
class 它的命名空间是 App\Expo
而你正在使用 App\Notifications\ExpoNotification
我正在为我的后端使用 Laravel 5.7(我是 Laravel 的新手)并且我正在尝试使用 Expo push notification extension for Laravel 向我的用户发送通知。
我按照说明的步骤进行操作,但我迷失了应该放置 class ExpoNotification extends Notification
的位置以及如何调用它。
我希望发生的是,每次订单状态发生变化时,都会向用户发送通知。
发生的事情是我收到一条错误消息,指出找不到 class
。
OrderController
public function update_order(Request $request, $id)
{
//Get the Order and update the status
Order::where('id', '=', $id )->update($request->only(['status']));
//Get the order with ::find so I can use $order->
$order = Order::find($id);
//Get user belonging to this order
$user= User::where('id', '=', $order->user_id);
//Get response with orders only posted the same day and are payed
$orders = Order::where('store_id', '=', $order->store_id)
->where('day', '=', $order->day )
->where('week', '=', $order->week )
->where('year', '=', $order->year )
->where('payment_status', '=', $order->payment_status)->get();
//send expo notification so the user gets his update
new ExpoNotification($order);
//return only relevant orders to the store
return OrderResource::collection($orders);
}
ExpoNotification
<?
namespace App\Notifications\ExpoNotification;
use App\Order;
use App\User;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
use Illuminate\Notifications\Notification;
class ExpoNotification extends Notification
{
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->body("Your {$notifiable->service} account was approved!");
}
}
邮递员出错
<!DOCTYPE html><!--
Symfony\Component\Debug\Exception\FatalThrowableError: Class 'App\Notifications\ExpoNotification' not found in file /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php on line 182
Stack trace:
1. Symfony\Component\Debug\Exception\FatalThrowableError->() /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php:182
回答 由穆罕默德提供
<?php
namespace App\Notifications;
use App\Order;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class ExNotification extends Notification
{
use Queueable;
protected $order;
public function __construct($order){
$this->order=$order;
}
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->body("Your {$notifiable->service} account was approved!");
}
public function toArray($notifiable)
{
return [
//
];
}
}
你的错误是你实现了你的 ExpoNotification
class 它的命名空间是 App\Expo
而你正在使用 App\Notifications\ExpoNotification