Core\WebPush\Sent::isGone
$sent->isGone(): boolReturns true when the push service no longer knows the subscription. The status is then 404 or 410.
A subscription is gone when the user removed the permission for notifications, or when the browser created a new subscription. The push service then deleted the old one. When isGone() is true, delete the subscription from your database too. Messages to it will never arrive. For every other status, isGone() is false. This includes an error such as 500 or 429, because the subscription still exists.
Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They check one subscription that is gone, show which status codes make isGone() true, and delete the old subscriptions from a list.
Return value
Returns bool. true for a subscription the service has deleted, and false otherwise.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\WebPush\Sent->isGone() checks whether the browser removed the subscription. The old name was gone. |
Examples
A subscription that is gone
<?nvs// Shows how to check that a browser's subscription no longer exists.
// The push service replies with 410, which means "gone". `Core\Test::answerHttp`// gives this fixed reply, so the example runs without a network.Core\Test::answerHttp('https://push.example.com/send/*', 410, {});
string $json = '{"endpoint":"https://push.example.com/send/old-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');
// `isGone()` is `true`, so this prints `The subscription is gone.`if ($sent->isGone()) { echo "The subscription is gone.\n";}The subscription is gone.Which codes are gone
<?nvs// Shows that `isGone()` is `true` only for the status codes 404 and 410.
// `Core\Test::answerHttp` gives four fixed replies, so the example runs without a network.Core\Test::answerHttp('https://push.example.com/send/201', 201, {});Core\Test::answerHttp('https://push.example.com/send/404', 404, {});Core\Test::answerHttp('https://push.example.com/send/410', 410, {});Core\Test::answerHttp('https://push.example.com/send/500', 500, {});
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);bytes $payload = Core\Encoding::encodeText('Hello', Core\Charset::Utf8);
// This prints one line per status. A 500 is an error, but the subscription still exists.array<string> $codes = ['201', '404', '410', '500'];foreach ($codes as string $code) { string $json = '{"endpoint":"https://push.example.com/send/' . $code . '","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 $sent->status(), ': gone is ', $sent->isGone() ? 'true' : 'false', "\n";}201: gone is false404: gone is true410: gone is true500: gone is falseDeleting old subscriptions
<?nvs// Shows a function that sends a message to every stored subscription and deletes each one that is gone.
class Subscriptions{ // Sends the message to each stored endpoint. Returns the endpoints to keep. public static function sendAndClean(array<string> $stored, string $text, Core\Crypto\KeyPair $vapid): array<string> { bytes $payload = Core\Encoding::encodeText($text, Core\Charset::Utf8); array<string> $keep = []; foreach ($stored as string $endpoint) { string $json = '{"endpoint":"' . $endpoint . '","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'); if ($sent->isGone()) { echo 'deleted: ', $endpoint, "\n"; } else { $keep[] = $endpoint; } } return $keep; }}
// The laptop's subscription returns 404, so the push service no longer knows it.Core\Test::answerHttp('https://push.example.com/send/phone', 201, {});Core\Test::answerHttp('https://push.example.com/send/laptop', 404, {});Core\Test::answerHttp('https://push.example.com/send/tablet', 201, {});
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);array<string> $stored = [ 'https://push.example.com/send/phone', 'https://push.example.com/send/laptop', 'https://push.example.com/send/tablet',];
// This prints the deleted endpoint, then `2 subscriptions kept`.array<string> $kept = Subscriptions::sendAndClean($stored, 'Your order has shipped.', $vapid);echo Core\Arr::count($kept), " subscriptions kept\n";deleted: https://push.example.com/send/laptop2 subscriptions kept