Laravel - How to use onesignal notification

15.9k Views Asked by At

I have a back office developed in laravel that allow to insert data that then android and ios app get.

Now i need to implement onsignal notification for example for when a new product was inserted in back office the app user receives a notification.

I setup my onesignal notification and install in my laravel project this package: https://github.com/laravel-notification-channels/onesignal. I set OneSignal App ID and REST API Key too.

After that I create a new controller and put the example code that is in package link. Controller:

   use Illuminate\Http\Request;
use NotificationChannels\OneSignal\OneSignalChannel;
use NotificationChannels\OneSignal\OneSignalMessage;
use NotificationChannels\OneSignal\OneSignalWebButton;
use Illuminate\Notifications\Notification;

class NotificationsController extends Controller
{
  public function via($notifiable)
  {
      return [OneSignalChannel::class];
  }

  public function toOneSignal($notifiable)
  {
      return OneSignalMessage::create()
          ->subject("Your {$notifiable->service} account was approved!")
          ->body("Click here to see details.")
          ->url('http://onesignal.com')
          ->webButton(
              OneSignalWebButton::create('link-1')
                  ->text('Click here')
                  ->icon('https://upload.wikimedia.org/wikipedia/commons/4/4f/Laravel_logo.png')
                  ->url('http://laravel.com')
          );
  }
}

But now I don't konw how to use it. For example, how can I send a notification when a new product was added? I need to setup routes?

Let me know if don't explain well my problem.

Thank you

1

There are 1 best solutions below

2
On

Hi you have to create a Custom Channel OneSignal Notification, so you don't have to do that on your Controller.

1.- First, you need to create a OneSignal Notification for each "event" that you want to notify

For instance when a Product was added

php artisan make:notification ProductAdded

This will generate a Notification File inside of App\Notifications\ProductAdded

2.- On the new File ProductAdded you will need to add that logic of the notification to OneSignal

<?php

// App\Notifications\ProductAdded.php

class OneSignal extends Notification
{
    use Queueable;
    private $data; //this is the "model" data that will be passed through the notify method

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [OneSignalChannel::class];
    }

    public function toOneSignal($notifiable)
    {
        //now you can build your message with the $this->data information
        return OneSignalMessage::create()
            ->subject("Your {$notifiable->service} account was approved!")
            ->body("Click here to see details.")
            ->url('http://onesignal.com')
            ->webButton(
                OneSignalWebButton::create('link-1')
                    ->text('Click here')
                    ->icon('https://upload.wikimedia.org/wikipedia/commons/4/4f/Laravel_logo.png')
                    ->url('http://laravel.com')
            );
    }

}

3.- Use the Notifiable trait in your Model

<?php

class YourModel extends Model
{
use Notifiable;

public function sendNotification()
{
    $this->notify(new ProductAdded($this)); //Pass the model data to the OneSignal Notificator
}

public function routeNotificationForOneSignal()
    {
        /*
         * you have to return the one signal player id tat will 
         * receive the message of if you want you can return 
         * an array of players id
         */

         return $this->data->user_one_signal_id;
    }

}
  1. Additionally you can use the Notification Facade wherever you want

$users it's a collection of player id's $data it's the data to build the notification

Notification::send($users, new ProductAdded($dataToNotify));

I hope this helps :)

Also you can read more of this stuff on Laravel Docs

https://laravel.com/docs/5.4/notifications

PD.

If you feel overwhelmed with the notification system, you can also use this package https://github.com/berkayk/laravel-onesignal it's a simple wrapper that you can use with ease and that has a lot of useful handy methods.