Untitled
unknown
plain_text
a year ago
7.2 kB
4
Indexable
<?php namespace App\Http\Libs; use Illuminate\Support\Facades\Log; use Kreait\Firebase\Exception\FirebaseException; use Kreait\Firebase\Exception\MessagingException; use Kreait\Firebase\Messaging\CloudMessage; use Kreait\Laravel\Firebase\Facades\Firebase; /** * Библиотека отправки пуш уведомления через FireBase */ class PushNotice { protected $fcm_token; protected $notification; public function __construct(?string $fcm_token) { $this->fcm_token = $fcm_token; $this->notification = Firebase::messaging(); } /** * Отправка пуш сообщения после онбординга резидента * * @param int $notice_id * @param string $author_name * @param string $author_surname * @param string $author_sex * @param int $author_id * @param int $badge * @return void */ public function onboarding(int $notice_id, string $author_name, string $author_surname, string $author_sex, int $author_id, int $badge = 0): void { $this->run([ 'notification' => [ 'title' => __('notice.push.user.onboarding.title'), 'body' => __('notice.push.user.onboarding.body', [ 'residentName' => $author_name, 'residentSurName' => $author_surname, 'assign' => $author_sex == 'woman' ? 'вступила' : 'вступил', ]), ], 'data' => [ 'type' => 'user.onboarding', 'notice_id' => $notice_id, 'user_id' => $author_id, ], 'apns' => [ 'payload' => [ 'aps' => [ 'badge' => $badge, ] ] ], 'priority' => "high", 'content_available' => true, ]); } /** * Отправка пуш сообщения после публикации отзыва * * @param int $notice_id * @param string $author_name * @param string $author_surname * @param string $author_sex * @param int $badge * @return void */ public function reviewCreated(int $notice_id, string $author_name, string $author_surname, string $author_sex, int $badge = 0): void { $this->run([ 'notification' => [ 'title' => __('notice.push.review.create.title'), 'body' => __('notice.push.review.create.body', [ 'residentName' => $author_name, 'residentSurName' => $author_surname, 'create' => $author_sex == 'woman' ? 'добавила' : 'добавил', ]), ], 'data' => [ 'type' => 'review.created', 'notice_id' => $notice_id, ], 'apns' => [ 'payload' => [ 'aps' => [ 'badge' => $badge, ] ] ], 'priority' => 'high', 'content_available' => true, ]); } /** * Отправка пуш сообщения после удаления отзыва * * @param int $notice_id * @param string $author_name * @param string $author_surname * @param string $author_sex * @param int $badge * @return void */ public function reviewDeleted(int $notice_id, string $author_name, string $author_surname, string $author_sex, int $badge = 0): void { $this->run([ 'notification' => [ 'title' => __('notice.push.review.delete.title'), 'body' => __('notice.push.review.delete.body', [ 'residentName' => $author_name, 'residentSurName' => $author_surname, 'delete' => $author_sex == 'woman' ? 'удалила' : 'удалил', ]), ], 'data' => [ 'type' => 'review.removed', 'notice_id' => $notice_id, ], 'apns' => [ 'payload' => [ 'aps' => [ 'badge' => $badge, ] ] ], 'priority' => "high", 'content_available' => true, ]); } /** * Опубликована новость * * @param int $notice_id * @param int $news_id * @param int $badge * @return void */ public function newsCreated(int $notice_id, int $news_id, int $badge = 0): void { $this->run([ 'notification' => [ 'title' => __('notice.push.news.create.title'), 'body' => __('notice.push.news.create.body'), ], 'data' => [ 'type' => 'news.created', 'notice_id' => $notice_id, 'newsId' => $news_id, ], 'apns' => [ 'payload' => [ 'aps' => [ 'badge' => $badge, ] ] ], 'priority' => "high", 'content_available' => true, ]); } /** * Отправка пуш сообщения после ответа на комментарий * * @param int $notice_id * @param int $news_id * @param int $root_comment_id * @param int $comment_id * @param int $badge * @return void */ public function replyToComment(int $notice_id, int $news_id, int $root_comment_id, int $comment_id, int $badge = 0): void { $this->run([ 'notification' => [ 'title' => __('notice.push.comment.create.title'), 'body' => __('notice.push.comment.create.body'), ], 'data' => [ 'type' => 'news.comment.created', 'notice_id' => $notice_id, 'newsId' => $news_id, 'rootCommentId' => $root_comment_id, 'commentId' => $comment_id, ], 'apns' => [ 'payload' => [ 'aps' => [ 'badge' => $badge, ] ] ], 'priority' => "high", 'content_available' => true, ]); } /** * Отправка пуш сообщения * * @param array $data * @return void */ private function run(array $data): void { try { if (is_null($this->fcm_token)) { throw new \Exception('Device token not set'); } $data['token'] = $this->fcm_token; $message = CloudMessage::fromArray($data); $this->notification->send($message); } catch (MessagingException|FirebaseException $e) { Log::error('PushNotice', [$e->getMessage()]); } catch (\Exception $e) { Log::error('PushNotice', [$e->getMessage()]); } } }
Editor is loading...
Leave a Comment