Skip to content

Core\WebPush\Sent::status

$sent->status(): int

Returns the HTTP status code that the push service sent back for one message, as an int.

201 means that the push service accepted the message. A code from 200 to 299 means that the send worked. 429 means "too many requests", so wait before you send to that browser again. A code of 400 or more is an error, but Core\WebPush::send does not throw an error for it. Your program reads status() and decides what to do. A redirect code such as 301 is also returned as it is, and send does not follow it.

Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They read the status of one message, tell an accepted message from one that was sent too often, and count the failed sends to several browsers.

Return value

Returns int. The status code, such as 201 for a message the service accepted.

Examples

Reading the status

<?nvs
// Shows how to read the status code that the push service returns for one message.
// `Core\Test::answerHttp` gives a fixed reply to this address, so the example runs
// without a network.
Core\Test::answerHttp('https://push.example.com/send/*', 201, {});
string $json = '{"endpoint":"https://push.example.com/send/phone","keys":{'
. '"p256dh":"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",'
. '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}';
Core\WebPush\Subscription $to = Core\WebPush::subscription($json);
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
bytes $payload = Core\Encoding::encodeText('Your order has shipped.', Core\Charset::Utf8);
Core\WebPush\Sent $sent = Core\WebPush::send($to, $payload, $vapid, 'mailto:admin@example.com');
// `status()` returns the code as an `int`. 201 means that the push service accepted
// the message. This prints `201`.
echo $sent->status(), "\n";
Output
201

Too many requests

<?nvs
// Shows how to tell a message that was accepted from a message that was sent too often.
class Push
{
// Returns a short report for one status code.
public static function report(int $status): string
{
if ($status == 201) {
return 'accepted';
}
// 429 means "too many requests". Wait before you send to this browser again.
if ($status == 429) {
return 'too many requests, try again later';
}
return 'failed with ' . $status;
}
}
// Two fixed replies, so the example runs without a network.
Core\Test::answerHttp('https://push.example.com/send/phone', 201, {});
Core\Test::answerHttp('https://push.example.com/send/laptop', 429, {});
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
bytes $payload = Core\Encoding::encodeText('New message', Core\Charset::Utf8);
// This prints one line per browser: the phone is accepted and the laptop is not.
array<string> $devices = ['phone', 'laptop'];
foreach ($devices as string $device) {
string $json = '{"endpoint":"https://push.example.com/send/' . $device . '","keys":{'
. '"p256dh":"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",'
. '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}';
Core\WebPush\Sent $sent = Core\WebPush::send(Core\WebPush::subscription($json), $payload, $vapid, 'mailto:admin@example.com');
echo $device, ': ', Push::report($sent->status()), "\n";
}
Output
phone: accepted
laptop: too many requests, try again later

Counting failures

<?nvs
// Shows a function that sends one message to several browsers and counts the sends that failed.
class Broadcast
{
// Sends the message to each endpoint. Returns how many sends had a status outside 200 to 299.
public static function failures(array<string> $endpoints, string $text, Core\Crypto\KeyPair $vapid): uint
{
bytes $payload = Core\Encoding::encodeText($text, Core\Charset::Utf8);
uint $failed = 0;
foreach ($endpoints as string $endpoint) {
string $json = '{"endpoint":"' . $endpoint . '","keys":{'
. '"p256dh":"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",'
. '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}';
int $status = Core\WebPush::send(Core\WebPush::subscription($json), $payload, $vapid, 'mailto:admin@example.com')->status();
if ($status < 200 || $status > 299) {
$failed = $failed + 1;
}
}
return $failed;
}
}
// Four fixed replies, so the example runs without a network.
Core\Test::answerHttp('https://push.example.com/send/1', 201, {});
Core\Test::answerHttp('https://push.example.com/send/2', 201, {});
Core\Test::answerHttp('https://push.example.com/send/3', 503, {});
Core\Test::answerHttp('https://push.example.com/send/4', 413, {});
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
array<string> $endpoints = [
'https://push.example.com/send/1',
'https://push.example.com/send/2',
'https://push.example.com/send/3',
'https://push.example.com/send/4',
];
// Two of the four sends failed, so this prints `2 of 4 failed`.
echo Broadcast::failures($endpoints, 'The sale starts now.', $vapid), ' of 4 failed', "\n";
Output
2 of 4 failed