Search code examples
laravelexpolaravel-5.7laravel-notification

Laravel 5.7 sending notifications using Expo


I'm using Laravel 5.7 for my backend (I'm new to Laravel) and I'm trying to use Expo push notification extension for Laravel to send notifications to my users.

I followed the steps explained, but I get lost to where I'm suppose to place the class ExpoNotification extends Notification and how to call it.

What I expect to happen is that every time an orders status get changed a notification is send to the user.

What is happening is that I get an error saying the class can't be found.

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!");
    }
}

Error from postman

<!DOCTYPE html><!--


Symfony\Component\Debug\Exception\FatalThrowableError: Class &#039;App\Notifications\ExpoNotification&#039; 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-&gt;() /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php:182

Answer Provided by Mohammed

<?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 [
            //
        ];
    }
}


Solution

  • Your wrong is your implementation of your ExpoNotification class it's namespace is App\Expo and you are using App\Notifications\ExpoNotification