sunaoka / push-notifications-php
Push notifications/messages for mobile devices
1.1.0
2024-12-13 02:55 UTC
Requires
- php: ^5.5 || ^7.0 || ^8.0
- ext-json: *
- ext-openssl: *
- google/apiclient: ^2.8
- guzzlehttp/guzzle: ^6.0 || ^7.0
- vlucas/valitron: ^1.4
Requires (Dev)
- phpunit/phpunit: ^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
README
Supported Protocols
Installation
composer require sunaoka/push-notifications-php
Basic Usage
For example, in the case of APNs Token Based.
<?php use Sunaoka\PushNotifications\Drivers\APNs; use Sunaoka\PushNotifications\Pusher; $payload = [ 'aps' => [ 'alert' => [ 'title' => 'Game Request', 'body' => 'Bob wants to play poker', ], ], ]; $options = new APNs\Token\Option(); $options->payload = $payload; $options->authKey = '/path/to/key.p8'; $options->keyId = 'ABCDE12345'; $options->teamId = 'ABCDE12345'; $options->topic = 'com.example.app'; $driver = new APNs\Token($options); $pusher = new Pusher(); $feedback = $pusher->to('Device token')->send($driver); $result = $feedback->isSuccess('Device token'); if (! $result) { echo $feedback->failure('Device token'); // BadDeviceToken }
How to specify options
There are two ways to specify the option.
$options = new APNs\Token\Option(); $options->payload = $payload; $options->authKey = '/path/to/key.p8'; $options->keyId = 'ABCDE12345'; $options->teamId = 'ABCDE12345'; $options->topic = 'com.example.app';
or
$options = new APNs\Token\Option([ 'payload' => $payload, 'authKey' => '/path/to/key.p8', 'keyId' => 'ABCDE12345', 'teamId' => 'ABCDE12345', 'topic' => 'com.example.app', ]);
Multicast message
Specify an array of device tokens in Pusher::to()
.
Then, you can distribute to multiple devices.
$pusher = new Pusher(); $feedback = $pusher->to([ 'Device token 1', 'Device token 2', 'Device token 3', ])->send($driver);
How to switch between the production and development environments (only APNs)
This is specified as an argument when creating an instance of Pusher
.
// Development environment (default) $pusher = new Pusher(false);
// Production environment $pusher = new Pusher(true);
Feedback
The return value of Pusher::send()
is a Feedback
object.
With the Feedback
object, you can determine whether the notification succeeded or failed.
$pusher = new Pusher(); $feedback = $pusher->to('Device token')->send($driver); $result = $feedback->isSuccess('Device token'); if ($result) { echo $feedback->success('Device token'); // 01234567-0123-0123-0123-01234567890A } else { echo $feedback->failure('Device token'); // BadDeviceToken }
HTTP Request Option
You can specify Guzzle Request Options as a driver option.
$options = new APNs\Token\Option(); $options->httpOptions = [ 'connect_timeout' => 3.14 'timeout' => 3.14, 'debug' => true, ];
More examples
More examples can be found in the examples directory.