3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Minishlink\WebPush\WebPush;
27 use Minishlink\WebPush\Subscription;
29 class PushSubscription
31 public static function execute(int $sid)
33 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
36 'subscription' => Subscription::create([
37 'endpoint' => $subscription['endpoint'],
38 'publicKey' => $subscription['pubkey'],
39 'authToken' => $subscription['secret'],
44 $webPush = new WebPush();
46 $report = $webPush->sendOneNotification(
47 $notification['subscription'],
48 $notification['payload']
51 $endpoint = $report->getRequest()->getUri()->__toString();
53 if ($report->isSuccess()) {
54 Logger::info('Message sent successfully for subscription', ['endpoint' => $endpoint]);
56 Logger::info('Message failed to sent for subscription', ['endpoint' => $endpoint, 'reason' => $report->getReason()]);