diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0549a3b7..fabee1ea 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,7 +9,8 @@ Changelog - Fix #358: Fix sending email notification to deleted users - Fix #361: Encoding issue - Enh #364: Confirm before leaving a filled message form -- Enh #367: Allow message title and body be provided by `GET` request for new messages +- Enh #367: Allow message title and body be provided by `GET` request for new messages +- Enh #368: Add push notifications when FCM Push Module is active 3.1.1 (September 19, 2023) --------------------------- diff --git a/models/MessageNotification.php b/models/MessageNotification.php index 76740175..9b9a384f 100644 --- a/models/MessageNotification.php +++ b/models/MessageNotification.php @@ -4,11 +4,13 @@ use humhub\modules\content\widgets\richtext\converter\RichTextToEmailHtmlConverter; use humhub\modules\content\widgets\richtext\converter\RichTextToHtmlConverter; +use humhub\modules\mail\helpers\Url; use humhub\modules\mail\live\NewUserMessage; use humhub\modules\mail\notifications\ConversationNotificationCategory; use humhub\modules\mail\notifications\MailNotificationCategory; use humhub\modules\notification\components\NotificationCategory; use humhub\modules\notification\targets\MailTarget; +use humhub\modules\notification\targets\MobileTarget; use humhub\modules\user\models\User; use Yii; use yii\base\BaseObject; @@ -54,6 +56,8 @@ public function notify(User $user) $isNewConversation = $this->isNewConversation; $this->sendMail($user); + + $this->sendPush($user); // Restore the flag $this->isNewConversation = $isNewConversation; @@ -97,6 +101,29 @@ private function canReceiveMail(User $user): bool return false; } + + private function canReceivePush(User $user): bool + { + if ($user->is($this->getEntrySender())) { + return false; + } + + if (!($mobileTarget = Yii::$app->notification->getTarget(MobileTarget::class))) { + return false; + } + + if ($mobileTarget->isCategoryEnabled($this->getNotificationCategory(), $user)) { + return true; + } + + // Try to send notification as "New message" when notification "New conversation" is disabled for the user + if ($this->isNewConversation && $mobileTarget->isCategoryEnabled(new MailNotificationCategory(), $user)) { + $this->isNewConversation = false; + return true; + } + + return false; + } private function getNotificationCategory(): NotificationCategory { @@ -135,6 +162,28 @@ private function sendMail(User $user) Yii::$app->i18n->autosetLocale(); } + + private function sendPush(User $user) + { + $fcmModule = Yii::$app->getModule('fcm-push'); + if (!$fcmModule || !$fcmModule->isActivated) { + return; + } + if (!$this->canReceivePush($user)) { + return; + } + + $firebaseService = new \humhub\modules\fcmPush\services\MessagingService($fcmModule->getConfigureForm()); + + $firebaseService->processMessage( + $user, + Yii::$app->name, + $this->getSubHeadline(), + Url::toMessenger($this->message, true), + null, + null + ); + } protected function getContent(User $user) { diff --git a/notifications/ConversationNotificationCategory.php b/notifications/ConversationNotificationCategory.php index 78f3e6e8..d8265a56 100644 --- a/notifications/ConversationNotificationCategory.php +++ b/notifications/ConversationNotificationCategory.php @@ -27,10 +27,8 @@ class ConversationNotificationCategory extends NotificationCategory public function getFixedSettings() { $webTarget = Yii::createObject(WebTarget::class); - $mobileTarget = Yii::createObject(MobileTarget::class); return [ - $webTarget->id, - $mobileTarget->id + $webTarget->id ]; } diff --git a/notifications/MailNotification.php b/notifications/MailNotification.php index 6f390753..530c4775 100644 --- a/notifications/MailNotification.php +++ b/notifications/MailNotification.php @@ -8,7 +8,6 @@ namespace humhub\modules\mail\notifications; - use humhub\modules\notification\components\BaseNotification; class MailNotification extends BaseNotification diff --git a/notifications/MailNotificationCategory.php b/notifications/MailNotificationCategory.php index b94bda13..ef98655b 100644 --- a/notifications/MailNotificationCategory.php +++ b/notifications/MailNotificationCategory.php @@ -21,10 +21,8 @@ class MailNotificationCategory extends NotificationCategory public function getFixedSettings() { $webTarget = Yii::createObject(WebTarget::class); - $mobileTarget = Yii::createObject(MobileTarget::class); return [ - $webTarget->id, - $mobileTarget->id + $webTarget->id ]; }