lundi 6 mai 2019

Unable to send a notification in Laravel

I'm not able to send a notification in Laravel. How should i do to resolve this error ?

Call to undefined method Illuminate\Database\Query\Builder::routeNotificationFor()

in Builder.php line 2123
at Builder->__call('routeNotificationFor', array('WebPush'))
at call_user_func_array(array(object(Builder), 'routeNotificationFor'), array('WebPush')) in Builder.php line 1015
at Builder->__call('routeNotificationFor', array('WebPush')) in WebPushChannel.php line 31
at WebPushChannel->send(object(Builder), object(PushDemo)) in ChannelManager.php line 77
at ChannelManager->sendNow(array(object(Builder)), object(PushDemo)) in ChannelManager.php line 43
at ChannelManager->send(array(object(Builder)), object(PushDemo)) in Facade.php line 217
at Facade::__callStatic('send', array(object(Builder), object(PushDemo))) in PushController.php line 36

My environment is a bit special : I'm building a PWA on Laravel 5.1

Notification backport with : laravel-notification-channels/backport

Webpush : laravel-notification-channels/webpush

I tried Notification facade and also user->notify()

My service worker works, manifest json is ok.

PushController :

    public function store(Request $request){
        $this->validate($request,[
            'endpoint'    => 'required',
            'keys.auth'   => 'required',
            'keys.p256dh' => 'required'
        ]);
        $endpoint = $request->endpoint;
        $token = $request->keys['auth'];
        $key = $request->keys['p256dh'];
        $user = Auth::user();
        $user->updatePushSubscription($endpoint, $key, $token);

        return response()->json(['success' => true],200);
    }

    public function push2(){
        Notification::send(User::all(),new PushDemo);
        return redirect()->back();
    }
    /**
     * Send Push Notification
     *
     * @return \Illuminate\Http\Response
     */
    public function push($id, Request $request){
//        Notification::send(User::all(),new PushDemo);
//        return redirect()->back();
        $user = User::findOrFail($id);
        $user->notify(new PushDemo());
        return redirect()->back();
    }

PushDemo extends Notification :

 public function via($notifiable)
    {
        return [WebPushChannel::class];
    }

public function toWebPush($notifiable, $notification)
    {
        return (new WebPushMessage)
            ->title('Hi dude')
            ->icon('images/icons/izzy-icon-192x192.png')
            ->body('Great, Push Notifications work!')
            ->action('View App', 'notification_action');

    }

User :

use Notifiable;
use HasPushSubscriptions;

Not able to send a notification, everytime getting 5xx errors. Did i miss anything ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire