From b861b85c36f2f0cd34633ab689f08b401d1b6dc0 Mon Sep 17 00:00:00 2001 From: yuriimaz Date: Tue, 15 Aug 2023 18:54:25 +0300 Subject: [PATCH] Enh #24: Notification Grant Status --- Events.php | 7 +---- assets/FcmPushAsset.php | 2 -- docs/CHANGELOG.md | 4 +++ resources/js/humhub.firebase.js | 45 ++++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Events.php b/Events.php index 07c851e..000d1dc 100644 --- a/Events.php +++ b/Events.php @@ -2,7 +2,6 @@ namespace humhub\modules\fcmPush; - use humhub\modules\fcmPush\assets\FcmPushAsset; use humhub\modules\fcmPush\assets\FirebaseAsset; use humhub\modules\fcmPush\components\NotificationTargetProvider; @@ -17,7 +16,6 @@ class Events { - private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout'; private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin'; @@ -82,7 +80,6 @@ public static function onNotificationInfoWidget($event) $baseStack = $event->sender; $baseStack->addWidget(PushNotificationInfoWidget::class); - } public static function onLayoutAddonInit($event) @@ -111,7 +108,6 @@ public static function onLayoutAddonInit($event) FcmPushAsset::register(Yii::$app->view); FirebaseAsset::register(Yii::$app->view); - } public static function onAfterLogout() @@ -123,5 +119,4 @@ public static function onAfterLogin() { Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1); } - -} \ No newline at end of file +} diff --git a/assets/FcmPushAsset.php b/assets/FcmPushAsset.php index 9dc1c41..4b01b9e 100644 --- a/assets/FcmPushAsset.php +++ b/assets/FcmPushAsset.php @@ -36,7 +36,5 @@ public static function register($view) return parent::register($view); } - - } } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9ad9e7a..8b73018 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,3 +1,7 @@ +Unreleased +----------------------------- +- Enh #24: Notification Grant Status + 2.0.0-beta.6 (August 1, 2023) ----------------------------- - Fix #25: Notification is not sent in user's language setting diff --git a/resources/js/humhub.firebase.js b/resources/js/humhub.firebase.js index 2574c8b..17ee6a9 100644 --- a/resources/js/humhub.firebase.js +++ b/resources/js/humhub.firebase.js @@ -22,6 +22,47 @@ humhub.module('firebase', function (module, require, $) { } }; + const getNotificationPermissionContent = function () { + if (!("Notification" in window)) { + return 'This browser does not support notifications.'; + } + console.log('Notification.permission:', Notification.permission); + return Notification.permission === "granted" + ? 'Push Notifications are active on this browser.
You can disable it in browser settings for this site.' + : 'You have blocked Push Notifications.
' + + (Notification.permission === 'denied' + ? 'You can enable it in browser settings for this site.' + : ' Click here to enable'); // 'default' + } + + const showNotificationPermissionWindow = function () { + function handlePermission(permission) { + // set the button to shown or hidden, depending on what the user answers + addPushNotificationPermissionsInfo(permission, true); + } + + // Let's check if the browser supports notifications + if (!("Notification" in window)) { + console.log("This browser does not support notifications."); + } else { + Notification.requestPermission().then((permission) => { + handlePermission(permission); + }); + } + } + + const addPushNotificationPermissionsInfo = function (permission, rewrite = false) { + if (rewrite) { + const contentContainer = document.getElementById('notificationPermissions'); + contentContainer.innerHTML = getNotificationPermissionContent() + } else { + const content = '
' + getNotificationPermissionContent() + '
'; + $('.layout-sidebar-container').prepend($(content)); + } + + $('#enablePushBtn').on('click', showNotificationPermissionWindow); + } + const afterServiceWorkerRegistration = function (registration) { //console.log("After Service Worker Registration"); //console.log(registration); @@ -33,6 +74,7 @@ humhub.module('firebase', function (module, require, $) { // Request for permission this.messaging.requestPermission().then(function () { //console.log('Notification permission granted.'); + addPushNotificationPermissionsInfo('granted'); that.messaging.getToken().then(function (currentToken) { if (currentToken) { @@ -48,6 +90,7 @@ humhub.module('firebase', function (module, require, $) { }); }).catch(function (err) { module.log.info('Could not get Push Notification permission!', err); + addPushNotificationPermissionsInfo('denied'); }); }; @@ -131,4 +174,4 @@ humhub.module('firebase', function (module, require, $) { function afterServiceWorkerRegistration(registration) { humhub.modules.firebase.afterServiceWorkerRegistration(registration); -} \ No newline at end of file +}