Derek Derek - 4 years ago 365
PHP Question

How to subscribe user with laravel notification channel for OneSignal

I am trying to use the laravel-notifications-channel/onesignal and I am having some problems with users in my laravel app set up to receive notifications. The documentation on the github page does not really cover how a user authenticates them self to receive a notification.

Even reading over the OneSignal docs for sending users to OneSignal is not working for me.

How do I set it up where when a user is using our web app they are notified to receive notifications and then I can send notifications to them using laravel notifications?

Here is my AssignedToTask Notification file:

<?php

namespace App\Notifications;

use App\Task;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\OneSignal\OneSignalChannel;
use NotificationChannels\OneSignal\OneSignalMessage;
use NotificationChannels\OneSignal\OneSignalWebButton;

class AssignedToTask extends Notification
{

use Queueable;

protected $task;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Task $task)
{
//
$this->task = $task;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 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')
);
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('You have been assigned a new task')
->line('You have a new task: ' . $this->task->title)
->action('View Task', url('tasks/' . $this->task->id));
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}


In my user model:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use HipsterJazzbo\Landlord\BelongsToTenants;
use Cmgmyr\Messenger\Traits\Messagable;

class User extends Authenticatable
{
use Notifiable;
use EntrustUserTrait;
use BelongsToTenants;
use Messagable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'company_id'
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'company_id'
];

....

public function routeNotificationForOneSignal()
{
return 'ONE_SIGNAL_PLAYER_ID';
}

public function routeNotificationForMail()
{
return $this->email_address;
}

}


enter image description here

How do I set and get the ONE_SIGNAL_PLAYER_ID in user model so a user accepts notifications and I can send them notifications?

Answer Source

EDIT - 2 Since you don't know what's happening is, Let me try to explain how you can work with OneSignal.

This is a push messaging system just like any other push notification system. (FCM (google), PubNub).

How it Works

  1. First goto OneSignal.Com Create your account, and then create an app for you. Once you create an app it will give you SDK for Mobile, where your consumers are.
  2. Now whenever your consumers install and start your app, they will notify your web server with their own unique id and user information.
  3. The information you received about the user is unique player_id which you will store in your database against that user.
  4. Now when you want to send a notification to any mobile app just call the API Post Notification method with player_id and OneSignal will send a push notification to that mobile app.

EDIT - 1

I think now i understand your confusion about Notifications with OneSignalChannel

Flow

  1. You already have the players_ids stored in your app database against every user.
  2. Now when you want to push a notification to a player, you just take that users player_id from db and push a notification to OneSignal.

Well you took this meaning literally. Which was causing issue

public function routeNotificationForOneSignal()
{
    return 'ONE_SIGNAL_PLAYER_ID';
}

From the error message this function should have return a unique id (UUID).

Change the return value to actual player id at OneSignalChannel

That's all my friend.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download