Skip to content

Commit

Permalink
Enh #170: Push notifications (#368)
Browse files Browse the repository at this point in the history
* send Push (first step)

* typo

* Update MessageNotification.php

* remove mobile fixed setting

* use absolute URL

* Update CHANGELOG.md

* Update MessageNotification.php
  • Loading branch information
felixhahnweilheim authored Jan 8, 2024
1 parent a1358ff commit 094c56a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
---------------------------
Expand Down
49 changes: 49 additions & 0 deletions models/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +56,8 @@ public function notify(User $user)
$isNewConversation = $this->isNewConversation;

$this->sendMail($user);

$this->sendPush($user);

// Restore the flag
$this->isNewConversation = $isNewConversation;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 1 addition & 3 deletions notifications/ConversationNotificationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

Expand Down
1 change: 0 additions & 1 deletion notifications/MailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace humhub\modules\mail\notifications;


use humhub\modules\notification\components\BaseNotification;

class MailNotification extends BaseNotification
Expand Down
4 changes: 1 addition & 3 deletions notifications/MailNotificationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

Expand Down

0 comments on commit 094c56a

Please sign in to comment.