PHP code example of ez-php / push

1. Go to this page and download the library: Download ez-php/push 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/ */

    

ez-php / push example snippets


use EzPhp\HttpClient\HttpClientServiceProvider;
use EzPhp\Push\PushServiceProvider;

$app->register(HttpClientServiceProvider::class);
$app->register(PushServiceProvider::class);

return [
    'driver' => getenv('PUSH_DRIVER') ?: 'null',
    'apns' => [
        'key_id' => getenv('PUSH_APNS_KEY_ID') ?: '',
        'team_id' => getenv('PUSH_APNS_TEAM_ID') ?: '',
        'bundle_id' => getenv('PUSH_APNS_BUNDLE_ID') ?: '',
        'private_key' => getenv('PUSH_APNS_PRIVATE_KEY') ?: '',
        'sandbox' => filter_var(getenv('PUSH_APNS_SANDBOX'), FILTER_VALIDATE_BOOLEAN),
    ],
    'fcm' => [
        'project_id' => getenv('PUSH_FCM_PROJECT_ID') ?: '',
        'client_email' => getenv('PUSH_FCM_CLIENT_EMAIL') ?: '',
        'private_key' => getenv('PUSH_FCM_PRIVATE_KEY') ?: '',
    ],
    'webpush' => [
        'private_key' => getenv('PUSH_WEBPUSH_PRIVATE_KEY') ?: '',
        'subject' => getenv('PUSH_WEBPUSH_SUBJECT') ?: '',
        'ttl' => (int) (getenv('PUSH_WEBPUSH_TTL') ?: 86400),
    ],
];

use EzPhp\Push\Push;
use EzPhp\Push\PushMessage;

Push::send('device-token', new PushMessage(
    title: 'Order shipped',
    body: 'Your order #42 is on its way.',
    data: ['order_id' => '42'],
    badge: 1,
    sound: 'default',
));

$results = Push::sendToMany(['token-a', 'token-b'], $message);

foreach ($results as $token => $error) {
    if ($error !== null) {
        // e.g. drop $token from storage if $error indicates it is unregistered
    }
}

Push::send($subscriptionJson, new PushMessage('Order shipped', 'On its way', ['order_id' => '42']));

use EzPhp\Push\Driver\ArrayDriver;
use EzPhp\Push\Push;
use EzPhp\Push\Pusher;

$driver = new ArrayDriver();
Push::setPusher(new Pusher($driver));

// ... exercise code under test ...

$messages = $driver->sentTo('device-token');
assert(count($messages) === 1);

Push::resetPusher();

use EzPhp\Push\PushDriverInterface;
use EzPhp\Push\PushMessage;

final class SmsDriver implements PushDriverInterface
{
    public function send(string $token, PushMessage $message): void
    {
        // deliver via your own provider
    }
}

$app->bind(PushDriverInterface::class, fn () => new SmsDriver());