PHP code example of pushpad / pushpad-php

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

    

pushpad / pushpad-php example snippets






Pushpad\Pushpad::$authToken = '5374d7dfeffa2eb49965624ba7596a09';
Pushpad\Pushpad::$projectId = 123; // set a default project (optional)

$signature = Pushpad\Pushpad::signatureFor((string) $currentUserId);

$response = Pushpad\Notification::create([
    // elds
    'title' => 'Website Name',
    'target_url' => 'https://example.com',
    'icon_url' => 'https://example.com/assets/icon.png',
    'badge_url' => 'https://example.com/assets/badge.png',
    'image_url' => 'https://example.com/assets/image.png',
    'ttl' => 604800,
    '
    'starred' => true,
    'send_at' => (new DateTimeImmutable('+1 hour'))->format(DATE_ATOM),
    'custom_metrics' => ['examples', 'another_metric'],

    // targeting options
    'uids' => ['user-1', 'user-2'],
    'tags' => ['segment1', 'segment2'],
]);

// Notification ID
$notificationId = $response['id'];

// Estimated number of devices that will receive the notification
// Not available for notifications that use send_at
$estimatedReach = $response['scheduled'];

// Available only if you specify some user IDs (uids) in the request:
// it indicates which of those users are subscribed to notifications.
// Not available for notifications that use send_at
$reachedUids = $response['uids'];

// The time when the notification will be sent.
// Available for notifications that use send_at
$scheduledAt = $response['send_at'];

$notification = Pushpad\Notification::find(42);

echo $notification->title; // "Foo Bar"
echo $notification->target_url; // "https://example.com"
echo $notification->ttl; // 604800
echo $notification->created_at; // ISO 8601 string
echo $notification->successfully_sent_count; // 4
echo $notification->opened_count; // 2

// ... and many other attributes
print_r($notification->toArray());

$notifications = Pushpad\Notification::findAll(['page' => 1]);

foreach ($notifications as $item) {
    printf("Notification %d: %s\n", $item->id, $item->title);
}

Pushpad\Notification::create([
    'body' => 'This notification will be sent after 60 seconds',
    'send_at' => (new DateTimeImmutable('+60 seconds'))->format(DATE_ATOM),
]);

$notification = Pushpad\Notification::find(5);
$notification->cancel();

$total = Pushpad\Subscription::count();
$byUser = Pushpad\Subscription::count(['uids' => ['user1']]);
$byTags = Pushpad\Subscription::count(['tags' => ['sports && travel']]);
$combined = Pushpad\Subscription::count(['uids' => ['user1'], 'tags' => ['sports && travel']], 5);

$subscriptions = Pushpad\Subscription::findAll(['tags' => ['sports'], 'page' => 2]);

foreach ($subscriptions as $subscription) {
    echo $subscription->id . PHP_EOL;
    // ...
}

$subscription = Pushpad\Subscription::find(123);

echo $subscription->id;
echo $subscription->project_id;
echo $subscription->endpoint;
echo $subscription->uid;
echo $subscription->tags;
echo $subscription->last_click_at;
echo $subscription->created_at;

// ... and many other attributes
print_r($subscription->toArray());

$subscriptions = Pushpad\Subscription::findAll(['uids' => ['user1']]);

foreach ($subscriptions as $subscription) {
    $tags = $subscription->tags ?? [];
    $tags[] = 'another_tag';

    $subscription->update([
        'uid' => 'myuser1',
        'tags' => array_values(array_unique($tags)),
    ]);
}

$subscription = Pushpad\Subscription::create([
    'endpoint' => 'https://example.com/push/f7Q1Eyf7EyfAb1',
    'p256dh' => 'BCQVDTlYWdl05lal3lG5SKr3VxTrEWpZErbkxWrzknHrIKFwihDoZpc_2sH6Sh08h-CacUYI-H8gW4jH-uMYZQ4=',
    'auth' => 'cdKMlhgVeSPzCXZ3V7FtgQ==',
    'uid' => 'exampleUid',
    'tags' => ['exampleTag1', 'exampleTag2'],
]);

$subscription = Pushpad\Subscription::find(123);
$subscription->delete();

$project = Pushpad\Project::create([
    'sender_id' => 123,
    'name' => 'My project',
    'website' => 'https://example.com',
    'icon_url' => 'https://example.com/icon.png',
    'badge_url' => 'https://example.com/badge.png',
    'notifications_ttl' => 604800,
    'notifications_

$sender = Pushpad\Sender::create([
    'name' => 'My sender',
    // omit the keys below to let Pushpad generate them automatically
    // 'vapid_private_key' => '-----BEGIN EC PRIVATE KEY----- ...',
    // 'vapid_public_key' => '-----BEGIN PUBLIC KEY----- ...',
]);

$senders = Pushpad\Sender::findAll();

$sender = Pushpad\Sender::find(987);
$sender->update(['name' => 'The New Sender Name']);
$sender->delete();
bash
composer 
bash
git clone https://github.com/pushpad/pushpad-php.git