Skip to main content

Installation

To install the ActivitySmith PHP SDK, you can use Composer:
PHP
composer require activitysmith/activitysmith

Usage

  1. Create an API key
  2. Set the API key as an environment variable named ACTIVITYSMITH_API_KEY or pass it directly to ActivitySmith.
Hereโ€™s an example of how to use the SDK with error handling:
PHP
<?php

declare(strict_types=1);

use ActivitySmith\ActivitySmith;

$apiKey = $_ENV['ACTIVITYSMITH_API_KEY'] ?? 'YOUR-API-KEY';
$activitysmith = new ActivitySmith($apiKey);

Send a Push Notification

Use \$activitysmith->notifications->send with a push payload. title is required; message and subtitle are optional. Push Notification
PHP
$response = $activitysmith->notifications->send([
    'title' => 'New subscription ๐Ÿ’ธ',
    'message' => 'Customer upgraded to Pro plan',
]);

echo $response->getSuccess() ? 'true' : 'false';
echo PHP_EOL;
echo $response->getDevicesNotified();

Start a Live Activity

Start a Live Activity with \$activitysmith->liveActivities->start. For a segmented progress activity, include title, number_of_steps, current_step, and type. Start Live Activity
PHP
$start = $activitysmith->liveActivities->start([
    'content_state' => [
        'title' => 'Nightly database backup',
        'subtitle' => 'create snapshot',
        'number_of_steps' => 3,
        'current_step' => 1,
        'type' => 'segmented_progress',
        'color' => 'yellow',
    ],
]);

$activityId = $start->getActivityId();

Update a Live Activity

Update a Live Activity with \$activitysmith->liveActivities->update using the activity_id. Update Live Activity
PHP
$update = $activitysmith->liveActivities->update([
    'activity_id' => $activityId,
    'content_state' => [
        'title' => 'Nightly database backup',
        'subtitle' => 'upload archive',
        'current_step' => 2,
    ],
]);

echo $update->getDevicesNotified();

End a Live Activity

End a Live Activity with \$activitysmith->liveActivities->end. You can optionally set auto_dismiss_minutes in the content_state. End Live Activity
PHP
$end = $activitysmith->liveActivities->end([
    'activity_id' => $activityId,
    'content_state' => [
        'title' => 'Nightly database backup',
        'subtitle' => 'verify restore',
        'current_step' => 3,
        'auto_dismiss_minutes' => 2,
    ],
]);

echo $end->getSuccess() ? 'true' : 'false';

Channels

You can target specific channels when sending a push or starting a Live Activity.
PHP
$activitysmith->notifications->send([
    'title' => 'New subscription ๐Ÿ’ธ',
    'message' => 'Customer upgraded to Pro plan',
    'channels' => ['ios-builds', 'engineering'],
]);

$activitysmith->liveActivities->start([
    'channels' => ['ios-builds'],
    'content_state' => [
        'title' => 'Nightly database backup',
        'number_of_steps' => 3,
        'current_step' => 1,
        'type' => 'segmented_progress',
    ],
]);

Error Handling

Handle API errors with try/catch around SDK calls.

Additional Resources