+ if (empty($subscription)) {
+ Logger::info('Subscription not found', ['subscription' => $sid]);
+ return;
+ }
+
+ try {
+ $Notification = DI::notification()->selectOneById($nid);
+ } catch (NotFoundException $e) {
+ Logger::info('Notification not found', ['notification' => $nid]);
+ return;
+ }
+
+ $application_token = DBA::selectFirst('application-token', [], ['application-id' => $subscription['application-id'], 'uid' => $subscription['uid']]);
+ if (empty($application_token)) {
+ Logger::info('Application token not found', ['application' => $subscription['application-id']]);
+ return;
+ }
+
+ $user = User::getById($Notification->uid);
+ if (empty($user)) {
+ Logger::info('User not found', ['application' => $subscription['uid']]);
+ return;
+ }
+
+ $l10n = DI::l10n()->withLang($user['language']);
+
+ if ($Notification->actorId) {
+ $actor = Contact::getById($Notification->actorId);
+ }
+
+ $body = '';
+
+ if ($Notification->targetUriId) {
+ $post = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]]);
+ if (!empty($post['body'])) {
+ $body = BBCode::toPlaintext($post['body'], false);
+ $body = Plaintext::shorten($body, 160, $Notification->uid);
+ }
+ }
+
+ $message = DI::notificationFactory()->getMessageFromNotification($Notification);
+ $title = $message['plain'] ?: '';
+
+ $push = Subscription::create([
+ 'contentEncoding' => 'aesgcm',
+ 'endpoint' => $subscription['endpoint'],
+ 'keys' => [
+ 'p256dh' => $subscription['pubkey'],
+ 'auth' => $subscription['secret']
+ ],
+ ]);
+
+ $payload = [
+ 'access_token' => $application_token['access_token'],
+ 'preferred_locale' => $user['language'],
+ 'notification_id' => $nid,
+ 'notification_type' => \Friendica\Factory\Api\Mastodon\Notification::getType($Notification),
+ 'icon' => $actor['thumb'] ?? '',
+ 'title' => $title ?: $l10n->t('Notification from Friendica'),
+ 'body' => $body ?: $l10n->t('Empty Post'),
+ ];
+
+ Logger::info('Payload', ['payload' => $payload]);