2017-08-29 83 views
0

我有一個在laravel開發的後臺辦公室,允許插入android和ios應用程序獲取的數據。Laravel - 如何使用onesignal通知

現在我需要實施onsignal通知,例如,當新產品插入後臺時,應用程序用戶收到通知。

我安裝了我的onesignal通知並安裝在我的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') 
     ); 
    } 
} 

但現在我卻不知道如何使用它。 例如,如何在添加新產品時發送通知? 我需要設置路線?

讓我知道如果不解釋我的問題。

謝謝

回答

2

嗨,你必須創建一個自定義頻道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這是一個簡單的包裝,您可以輕鬆使用,並且有許多有用的方便方法。

    +0

    方法「routeNotificationForOneSignal」應放置在Notifiable模型中,在這種情況下可能是用戶模型。請參閱http://laravel-notification-channels.com/onesignal/ – Johan