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;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Subscription as ModelSubscription;
29 use Friendica\Util\DateTimeFormat;
30 use Minishlink\WebPush\WebPush;
31 use Minishlink\WebPush\Subscription;
33 class PushSubscription
35 public static function execute(int $sid, int $nid)
37 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
38 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
40 if (!empty($notification['uri-id'])) {
41 $notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
44 if (!empty($notification['actor-id'])) {
45 $actor = Contact::getById($notification['actor-id']);
49 'subscription' => Subscription::create([
50 'endpoint' => $subscription['endpoint'],
51 'publicKey' => $subscription['pubkey'],
52 'authToken' => $subscription['secret'],
54 // @todo Check if we are supposed to transmit a payload at all
55 'payload' => json_encode([
56 'title' => 'Friendica',
57 'body' => $notify['msg'] ?? '',
58 'icon' => $actor['thumb'] ?? '',
60 'badge' => DI::baseUrl()->get() . '/images/friendica-192.png',
61 'tag' => $notification['parent-uri-id'] ?? '',
62 'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON),
68 'subject' => DI::baseUrl()->getHostname(),
69 'publicKey' => ModelSubscription::getPublicVapidKey(),
70 'privateKey' => ModelSubscription::getPrivateVapidKey(),
74 $webPush = new WebPush($auth);
76 $report = $webPush->sendOneNotification(
77 $push['subscription'],
81 $endpoint = $report->getRequest()->getUri()->__toString();
83 if ($report->isSuccess()) {
84 Logger::info('Message sent successfully for subscription', ['endpoint' => $endpoint]);
86 Logger::info('Message failed to sent for subscription', ['endpoint' => $endpoint, 'reason' => $report->getReason()]);