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
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 OneSignal3.- Use the
Notifiable
trait in your ModelNotification
Facade wherever you want$users
it's a collection of player id's$data
it's the data to build the notificationNotification::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.