web-push-libs / web-push-php

Web Push library for PHP
MIT License
1.68k stars 295 forks source link

Get subscription from report (in case of error) #357

Closed MaartenW closed 1 year ago

MaartenW commented 2 years ago

NOTE: Please test in a least two browsers (i.e. Chrome and Firefox). This helps with diagnosing problems quicker.

Please confirm the following:

Setup

Please provide the following details, the more info you can provide the better.

Please check that you have installed and enabled these PHP extensions :

Please select any browsers that you are experiencing problems with:

Server side, general concept.

Problem

When iterating over reports after $webPush->flush() how to find which subscription the report belongs to? I'd like to clean any 410 Gone subscriptions.

Expected

I would expect something like $report->getSubscription() to be able to get the subscription from the report and handle errors.

Features Used

Example / Reproduce Case

/**
* Check sent results
* @var MessageSentReport $report
*/
foreach ($webPush->flush() as $report) {
  $endpoint = $report->getRequest()->getUri()->__toString();

  if (!$report->isSuccess()) {

    // Here you want to handle 410 Gone errors by disabling the subscription (e.g. in the dbase)

    $sub = $report->getSubscription();
    handleExpiredSubscription($sub);
  }
}

Other

Please put any remaining notes here.

andy-UKC commented 1 year ago

FWIW I hash the endpoint, each should be unique, and associate it with a subscription. Then when you flush at the end, hash the endpoint URI of all 410s, then use it to get the associated subscription. Ideally, the $report->getSubscription() method would exist, but so long as endpoints remain unique, there shouldn't be an issue with my way

MaartenW commented 1 year ago

Thanks @andy-UKC, I think that would easily solve it.