Laravel - 如何使用信号通知

use*_*861 2 php notifications laravel onesignal

我有一个在laravel中开发的后台,允许插入android和ios app获得的数据.

现在我需要实施onsignal通知,例如,当在后台插入新产品时,app用户会收到通知.

我设置了我的信号通知并在我的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')
          );
  }
}
Run Code Online (Sandbox Code Playgroud)

但现在我不知道如何使用它.例如,如何在添加新产品时发送通知?我需要设置路线?

如果不解释我的问题,请告诉我.

谢谢

OsD*_*Dev 8

您好,您必须创建自定义频道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')
            );
    }

}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)
  1. 此外,您可以Notification随时随地使用Facade

$users它是玩家ID的集合, $data它是构建通知的数据

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

我希望这有帮助 :)

您还可以在Laravel Docs上阅读更多这些内容

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

PD.

如果你对通知系统感到不知所措,你也可以使用这个软件包 https://github.com/berkayk/laravel-onesignal它是一个简单的包装器,你可以轻松使用它,并且有很多有用的方法.

  • 方法“routeNotificationForOneSignal”应该放在 Notifiable 模型中,在这种情况下可能是 User 模型。见 http://laravel-notification-channels.com/onesignal/ (2认同)
  • 'routeNotificationForOneSignal' 方法不应该在通知类中,它应该在调用通知的类中,...至少对于 laravel 5.5 (2认同)