Laravel - 如何使用 onesignal 通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45942865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - How to use onesignal notification
提问by user3242861
I have a back office developed in laravel that allow to insert data that then android and ios app get.
我有一个在 laravel 中开发的后台,它允许插入数据,然后 android 和 ios 应用程序获取。
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.
现在我需要实现 onsignal 通知,例如,当新产品插入后台时,应用程序用户会收到通知。
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.
设置我的onesignal通知,并在我的laravel项目安装该软件包:https://github.com/laravel-notification-channels/onesignal
。我设置OneSignal App ID
和REST API Key
太。
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
谢谢
回答by OsDev
Hi you have to create a Custom Channel OneSignal Notification, so you don't have to do that on your Controller.
您好,您必须创建自定义频道 OneSignal 通知,因此您不必在控制器上执行此操作。
1.- First, you need to create a OneSignal Notification for each "event" that you want to notify
1.- 首先,您需要为要通知的每个“事件”创建一个 OneSignal 通知
For instance when a Product was added
例如,当添加产品时
php artisan make:notification ProductAdded
php artisan make:notification ProductAdded
This will generate a Notification File inside of App\Notifications\ProductAdded
这将在其中生成一个通知文件 App\Notifications\ProductAdded
2.- On the new File ProductAdded
you will need to add that logic of the notification to OneSignal
2.- 在新文件上,ProductAdded
您需要将该通知逻辑添加到 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
3.-Notifiable
在您的模型中使用特征
<?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;
}
}
- Additionally you can use the
Notification
Facade wherever you want
- 此外,您可以在
Notification
任何地方使用Facade
$users
it's a collection of player id's
$data
it's the data to build the notification
$users
它是玩家 ID 的集合,
$data
它是构建通知的数据
Notification::send($users, new ProductAdded($dataToNotify));
Notification::send($users, new ProductAdded($dataToNotify));
I hope this helps :)
我希望这有帮助 :)
Also you can read more of this stuff on Laravel Docs
你也可以在 Laravel Docs 上阅读更多这些东西
https://laravel.com/docs/5.4/notifications
https://laravel.com/docs/5.4/notifications
PD.
PD。
If you feel overwhelmed with the notification system, you can also use this package https://github.com/berkayk/laravel-onesignalit's a simple wrapper that you can use with ease and that has a lot of useful handy methods.
如果你对通知系统感到不知所措,你也可以使用这个包 https://github.com/berkayk/laravel-onesignal它是一个简单的包装器,你可以轻松使用,并且有很多有用的方便方法。