1. Go to this page and download the library: Download mingalevme/onesignal 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/ */
mingalevme / onesignal example snippets
declare(strict_types=1);
use Mingalevme\OneSignal\ClientFactory;
use Mingalevme\OneSignal\ClientFactoryInterface;
use Mingalevme\OneSignal\ClientInterface;
use Mingalevme\OneSignal\CreateClientOptions;
class AppServiceProvider
{
public function register(SomeConfigClass $someConfig, SomeDIContainerClass $container): void
{
// ...
/** @var string $appId */
$appId = $someConfig->get('onesignal-app-id');
/** @var string $restApiKey */
$restApiKey = $someConfig->get('my-rest-api-key');
$container->set(ClientFactoryInterface::class, ClientFactory::class);
$container->set(
ClientInterface::class,
fn() => $container->get(ClientFactoryInterface::class)
->create(CreateClientOptions::new($appId, $restAPIKey))
);
// ...
}
}
declare(strict_types=1);
use Mingalevme\OneSignal\ClientInterface;
use Mingalevme\OneSignal\CreateNotificationOptions;
use Mingalevme\OneSignal\Notification\PushNotification;
class NotifySubscribersOnPostCreatedEventHandler
{
private SubscribersRespo $repo;
private ClientInterface $client;
public function __construct(SubscribersRepo $repo, ClientInterface $client)
{
$this->repo = $repo;
$this->client = $client;
}
public function handle(PostCreatedEvent $event): void
{
$post = $event->getPost();
/** @var string[] $subscriberIds */
$subscriberIds = $this->repo->getSubscriberIdsOfAuthor($post->getAuthorId());
$notification = PushNotification::createContentsNotification($post->getTitle())
->setHeadings($post->getAuthorDisplayName())
->setData([
'post_id' => $post->getId(),
])
->setExternalId("post-{$post->getId()}")
->setIncludeExternalUserIds($subscriberIds);
$this->client->createNotification($notification);
}
}
declare(strict_types=1);
use GuzzleHttp\Psr7\HttpFactory;
use Mingalevme\OneSignal\Client;
use Mingalevme\OneSignal\CreateClientOptions;
use Mingalevme\OneSignal\CreateNotificationOptions;
use Mingalevme\OneSignal\Notification\PushNotification;
$appId = 'my-app-id';
$restApiKey = 'my-rest-api-key';
$psrHttpClient = new \GuzzleHttp\Client();
$psr7Factory = new HttpFactory();
$client = new Client(CreateClientOptions::new($appId, $restAPIKey), $psrHttpClient, $psr7Factory, $psr7Factory);
$notification = PushNotification::createContentsNotification('text')
->setData([
'type' => 'my-notification-type',
'data' => 'some-extra-data',
])
->addFilterTagExists('tag1')
->addFilterTag('tag2', '>', 'value2')
->setSmallIcon('push_icon')
->setTtl(3600 * 3)
->setIosCategory('my-ios-category')
->setExternalId('custom-notification-id');
$result = $client->createNotification($notification);
echo <<<END
Notification has been successfully sent to OneSignal: #{$result->getNotificationId()}.
END;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.