web-push-libs / web-push-php

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

how i can get my custom key for each output of `$webPush->flush()` ? #309

Closed ziaratban closed 3 years ago

ziaratban commented 3 years ago

i want to use the batch feature for more performance. so i need set the key for each subscription and get that key in the result.

for example

foreach(...) {
  $webPush->queueNotification(..., ['myKey' => myValue]),
}
foreach ($webPush->flush() as $report) {
     $report->customData['myKey']; # <----
}
AndrewGalaz23 commented 3 years ago

At first, I thought I had the same need as you. Then I remembered that the endpoint uniquely identifies a push subscription.

From the W3C Push API specification:

A push subscription has an associated push endpoint. It MUST be the absolute URL exposed by the push service where the application server can send push messages to. A push endpoint MUST uniquely identify the push subscription.

So, you can use the getEndpoint method to retrieve the endpoint of the request and then use it as unique key:

foreach($webpush->flush() as $report) {
  $endpoint = $report->getEndpoint();
  //Check if the subscription is invalid or expired.
  if($report->isSubscriptionExpired()) {
    //Delete subscription from db using $endpoint as UK.
  }
}

Hope this can help you!

8Ozymandias commented 3 years ago

Hello @ziaratban and @AndrewGalaz23 are you aware if it's possible that when one clicks on a notification if it's possible that it can open up the browser and go to a specific website and or section on that website?

Minishlink commented 3 years ago

Yes that's it!

xPaw commented 1 year ago

While endpoint is good, I do think passing custom data would be helpful here, for example when you're sending multiple notifications to the same endpoint.