Laravel - 如何使用onesignal通知
我有一个在laravel开发的后台办公室,允许插入android和ios应用程序获取的数据。Laravel - 如何使用onesignal通知
现在我需要实施onsignal通知,例如,当新产品插入后台时,应用程序用户收到通知。
我安装了我的onesignal通知并安装在我的laravel项目中:https://github.com/laravel-notification-channels/onesignal
。 我也设置了OneSignal App ID
和REST API Key
。
之后,我创建一个新的控制器,并把封装链接中的示例代码。 控制器:
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')
);
}
}
但现在我却不知道如何使用它。 例如,如何在添加新产品时发送通知? 我需要设置路线?
让我知道如果不解释我的问题。
谢谢
嗨,你必须创建一个自定义频道OneSignal通知,所以你没有做你的控制器。
1.-首先,您需要创建要通知
例如,当添加一个产品每一个“事件”一OneSignal通知
php artisan make:notification ProductAdded
,这将产生一个通知文件App\Notifications\ProductAdded
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')
);
}
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;
}
}
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
}
}
- 此外,您可以使用
Notification
门面,无论你想
$users
这是玩家ID的集合 $data
这是生成通知的数据
Notification::send($users, new ProductAdded($dataToNotify));
我希望这有助于:)
您也可以了解更多这方面的东西对Laravel文档
https://laravel.com/docs/5.4/notifications
PD。
如果您对通知系统感到不知所措,您也可以使用此包https://github.com/berkayk/laravel-onesignal这是一个简单的包装,您可以轻松使用,并且有许多有用的方便方法。
方法“routeNotificationForOneSignal”应放置在Notifiable模型中,在这种情况下可能是用户模型。请参阅http://laravel-notification-channels.com/onesignal/ – Johan