1. Go to this page and download the library: Download yashcli/onesignal-notifier library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use YashCli\OneSignalNotifier\OneSignal;
$oneSignal = new OneSignal();
$result = $oneSignal
->setContent(['en' => 'Hello! This is a test notification.'])
->setHeading(['en' => 'Test Notification'])
->setSubscriptionIds(['subscription_id_1', 'subscription_id_2'])
->send();
use Illuminate\Notifications\Notification;
use YashCli\OneSignalNotifier\Channels\OneSignalChannel;
class TestNotification extends Notification
{
public function via($notifiable): array
{
return [OneSignalChannel::class];
}
public function toOneSignal($notifiable): array
{
return [
'content' => ['en' => 'You have a new message!'],
'heading' => ['en' => 'New Message'],
'subscription_ids' => ['subscription_id_1'],
'priority' => 10,
'images' => [
'big_picture' => 'https://example.com/image.jpg',
],
'custom_data' => [
'message_id' => '12345',
'sender' => 'John Doe',
],
];
}
}
use YashCli\OneSignalNotifier\OneSignal;
$client = OneSignal::createClient('your-app-id', 'your-rest-api-key');
$result = $client
->setContent(['en' => 'Hello from a custom client!'])
->setHeading(['en' => 'Custom Client'])
->setSubscriptionIds(['subscription_id_1'])
->send();
// In your User model (or any Notifiable model)
public function routeNotificationForOneSignal()
{
// Return a single player ID (deprecated)
// return 'PLAYER_ID';
// Return an array of player IDs (deprecated)
// return ['PLAYER_ID_1', 'PLAYER_ID_2'];
// Return subscription IDs (recommended)
// return ['subscription_id_1', 'subscription_id_2'];
// Return external user IDs
// return ['
// User model
public function routeNotificationForOneSignal()
{
return ['subscription_id_1', 'subscription_id_2'];
}
// Notification class (no need to specify recipient IDs)
public function toOneSignal($notifiable): array
{
return [
'content' => ['en' => 'You have a new message!'],
'heading' => ['en' => 'New Message'],
// No subscription_ids needed here!
];
}