Laravel-如何使用单信号通知 - php

我在laravel中开发了一个后台,可以插入数据,然后android和ios应用程序获取。

现在,我需要实现信号通知,例如,当新产品插入后台时,应用程序用户会收到通知。

我设置了一个单信号通知,并在我的laravel项目中安装了以下软件包:https://github.com/laravel-notification-channels/onesignal
我也设置了OneSignal App IDREST 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通知,因此不必在Controller上执行此操作。

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')
            );
    }

}

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;
    }

}

另外,您可以在任意位置使用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,它是一个简单的包装,可以轻松使用,并且有很多有用的方便方法。

检查对象是否已在集合中-Laravel - php

当我循环一系列不同的结果时,我希望将对象添加到新集合中。查询:$osRed = Item::where('category', 'Hardware') ->where(function ($query) { $query->where('operating_system', '…

Laravel Blade模板尝试获取非对象的属性时如何返回null而不是ErrorException - php

我正在编写一些Laravel Blade模板,并且我的模型可能包含空对象。我非常想尝试获取对象属性,如果有错误,则返回null。因此,不必编写以下代码:@if ($model->child_object_that_may_be_null) {{ $model->child_object_that_may_be_null->interesti…

PHP Laravel从另一个驱动器读取文件 - php

我目前正在学习Laravel。我想知道是否有一种方法可以在Laravel中使用Storage::从另一个硬盘访问文件(使用Windows)。例如,我在驱动器C:上安装了带有Laravel的Xampp,但是我想访问网站目录之外的E:上的文件。我试过使用Storage::files('E:')和File::files('E:…

Laravel-foreach局部变量正在覆盖全局变量 - php

今天,我面临一个非常奇怪的问题。我有一个控制器,将从该控制器向视图sections thread和threads发送三个变量。在我看来,我正在使用foreach循环遍历所有部分,如下所示:@foreach($sections as $i => $section) 对于每个部分,我都将创建一个div并给出如下的ID:id="{{ $thread…

Laravel 5排序具有关系的雄辩模型 - php

我有一个要基于关系属性进行排序的模型。主模型Resultado具有类型respondente的关系belongsTo,而respondente具有关系usuario。我想获取具有这些关系的Resultado并按usuario name属性(列)对结果进行排序。我试过了$resultados = Diagnostico\Resultado::with(…