Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh #24: Notification Grant Status #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +16,6 @@

class Events
{

private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout';
private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin';

Expand Down Expand Up @@ -82,7 +80,6 @@ public static function onNotificationInfoWidget($event)
$baseStack = $event->sender;

$baseStack->addWidget(PushNotificationInfoWidget::class);

}

public static function onLayoutAddonInit($event)
Expand Down Expand Up @@ -111,7 +108,6 @@ public static function onLayoutAddonInit($event)

FcmPushAsset::register(Yii::$app->view);
FirebaseAsset::register(Yii::$app->view);

}

public static function onAfterLogout()
Expand All @@ -123,5 +119,4 @@ public static function onAfterLogin()
{
Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1);
}

}
}
2 changes: 0 additions & 2 deletions assets/FcmPushAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ public static function register($view)

return parent::register($view);
}


}
}
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 44 additions & 1 deletion resources/js/humhub.firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>You can disable it in browser settings for this site.'
: 'You have blocked Push Notifications.<br>' +
(Notification.permission === 'denied'
? 'You can enable it in browser settings for this site.'
: '<a href="#" id="enablePushBtn"><i class="fa fa-unlock"></i> Click here to enable</a>'); // '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 = '<div class="panel panel-default panel-pn-permissions"><div class="panel-body" id="notificationPermissions">' + getNotificationPermissionContent() + '</div></div>';
$('.layout-sidebar-container').prepend($(content));
}

$('#enablePushBtn').on('click', showNotificationPermissionWindow);
}

const afterServiceWorkerRegistration = function (registration) {
//console.log("After Service Worker Registration");
//console.log(registration);
Expand All @@ -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) {
Expand All @@ -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');
});
};

Expand Down Expand Up @@ -131,4 +174,4 @@ humhub.module('firebase', function (module, require, $) {

function afterServiceWorkerRegistration(registration) {
humhub.modules.firebase.afterServiceWorkerRegistration(registration);
}
}