-
Notifications
You must be signed in to change notification settings - Fork 6
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: Grant Statuses #44
Open
ArchBlood
wants to merge
23
commits into
humhub:enh/show-push-status
Choose a base branch
from
ArchBlood:patch-1
base: enh/show-push-status
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b861b85
Enh #24: Notification Grant Status
serh-mosk 7849d33
Enh: Grant Statuses
ArchBlood 2398b95
Update CHANGELOG.md
ArchBlood da92e33
Update humhub.firebase.js
ArchBlood 9345b2b
Update FcmPushAsset.php
ArchBlood 54bb9ce
Update FcmPushAsset.php
ArchBlood a2d678d
Update humhub.firebase.js
ArchBlood afbb62f
Fix: Remove widget creation on dashboard
ArchBlood dde69ab
Update PushNotificationInfoWidget.php
ArchBlood ee054b0
Update push-notification-info.php
ArchBlood a511661
Update Events.php
ArchBlood a8ac703
Update config.php
ArchBlood 94d4f87
Update FcmPushAsset.php
ArchBlood f3bbe37
Fix: Console Errors
ArchBlood 35a16cc
Update Events.php
ArchBlood 15f4657
Update config.php
ArchBlood 8b3fea2
Merge branch 'master' into patch-1
ArchBlood f24667b
Update FcmPushAsset.php
ArchBlood 441f9a4
Update humhub.firebase.js
ArchBlood 56b77e7
Update config.php
ArchBlood ce0dc27
Update humhub.firebase.js
ArchBlood 964babe
Update humhub.firebase.js
ArchBlood c33f361
Update humhub.firebase.js
ArchBlood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
/** | ||
* @fileoverview HumHub Firebase Module | ||
* This module handles Firebase Cloud Messaging (FCM) integration for HumHub. | ||
* It manages notification permissions, token handling, and message reception. | ||
* @module firebase | ||
*/ | ||
|
||
humhub.module('firebase', function (module, require, $) { | ||
let messaging; | ||
|
||
/** | ||
* Initializes the Firebase module. | ||
* Sets up the Firebase app and messaging, and defines behavior for message reception and token refresh. | ||
* @function | ||
*/ | ||
const init = function () { | ||
if (!firebase.apps.length) { | ||
firebase.initializeApp({messagingSenderId: this.senderId()}); | ||
firebase.initializeApp({ messagingSenderId: this.senderId() }); | ||
this.messaging = firebase.messaging(); | ||
|
||
this.messaging.onMessage(function (payload) { | ||
module.log.info("Received FCM Push Notification", payload); | ||
}); | ||
|
||
// Callback fired if Instance ID token is updated. | ||
this.messaging.onTokenRefresh(function () { | ||
this.messaging.getToken().then(function (refreshedToken) { | ||
this.deleteTokenLocalStore(); | ||
|
@@ -22,21 +33,49 @@ humhub.module('firebase', function (module, require, $) { | |
} | ||
}; | ||
|
||
/** | ||
* Gets the content to display for notification permission status. | ||
* @function | ||
* @returns {string} The message corresponding to the current notification permission status. | ||
*/ | ||
const getNotificationPermissionContent = function () { | ||
if (!("Notification" in window)) { | ||
return module.text('status.not-supported'); | ||
} | ||
switch (Notification.permission) { | ||
case "granted": | ||
return module.text('status.granted'); | ||
case "denied": | ||
return module.text('status.denied'); | ||
default: | ||
return module.text('status.default'); | ||
} | ||
} | ||
|
||
/** | ||
* Adds information about push notification permissions to the UI. | ||
* @function | ||
*/ | ||
const addPushNotificationPermissionsInfo = function () { | ||
let content = getNotificationPermissionContent(); | ||
$('#pushNotificationPermissionInfo').html(content); | ||
} | ||
|
||
/** | ||
* Handles actions after the service worker registration. | ||
* @function | ||
* @param {ServiceWorkerRegistration} registration - The service worker registration object. | ||
*/ | ||
const afterServiceWorkerRegistration = function (registration) { | ||
//console.log("After Service Worker Registration"); | ||
//console.log(registration); | ||
|
||
const that = this; | ||
|
||
this.messaging.useServiceWorker(registration); | ||
|
||
// Request for permission | ||
this.messaging.requestPermission().then(function () { | ||
//console.log('Notification permission granted.'); | ||
addPushNotificationPermissionsInfo('granted'); | ||
|
||
that.messaging.getToken().then(function (currentToken) { | ||
if (currentToken) { | ||
//console.log('Token: ' + currentToken); | ||
that.sendTokenToServer(currentToken); | ||
} else { | ||
module.log.info('No Instance ID token available. Request permission to generate one.'); | ||
|
@@ -48,20 +87,23 @@ humhub.module('firebase', function (module, require, $) { | |
}); | ||
}).catch(function (err) { | ||
module.log.info('Could not get Push Notification permission!', err); | ||
addPushNotificationPermissionsInfo(Notification.permission); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, the parameter is not required right? |
||
}); | ||
}; | ||
|
||
// Send the Instance ID token your application server, so that it can: | ||
// - send messages back to this app | ||
// - subscribe/unsubscribe the token from topics | ||
/** | ||
* Sends the FCM token to the server. | ||
* @function | ||
* @param {string} token - The FCM token. | ||
*/ | ||
const sendTokenToServer = function (token) { | ||
const that = this; | ||
if (!that.isTokenSentToServer(token)) { | ||
module.log.info("Send FCM Push Token to Server"); | ||
$.ajax({ | ||
method: "POST", | ||
url: that.tokenUpdateUrl(), | ||
data: {token: token}, | ||
data: { token: token }, | ||
success: function (data) { | ||
that.setTokenLocalStore(token); | ||
} | ||
|
@@ -71,14 +113,29 @@ humhub.module('firebase', function (module, require, $) { | |
} | ||
}; | ||
|
||
/** | ||
* Checks if the token has already been sent to the server. | ||
* @function | ||
* @param {string} token - The FCM token. | ||
* @returns {boolean} Whether the token has been sent to the server. | ||
*/ | ||
const isTokenSentToServer = function (token) { | ||
return (this.getTokenLocalStore() === token); | ||
}; | ||
|
||
/** | ||
* Deletes the locally stored FCM token. | ||
* @function | ||
*/ | ||
const deleteTokenLocalStore = function () { | ||
window.localStorage.removeItem('fcmPushToken_' + this.senderId()) | ||
}; | ||
|
||
/** | ||
* Sets the FCM token in local storage. | ||
* @function | ||
* @param {string} token - The FCM token. | ||
*/ | ||
const setTokenLocalStore = function (token) { | ||
const item = { | ||
value: token, | ||
|
@@ -87,10 +144,14 @@ humhub.module('firebase', function (module, require, $) { | |
window.localStorage.setItem('fcmPushToken_' + this.senderId(), JSON.stringify(item)) | ||
}; | ||
|
||
/** | ||
* Gets the FCM token from local storage. | ||
* @function | ||
* @returns {string|null} The FCM token or null if it doesn't exist or is expired. | ||
*/ | ||
const getTokenLocalStore = function () { | ||
const itemStr = window.localStorage.getItem('fcmPushToken_' + this.senderId()) | ||
|
||
// if the item doesn't exist, return null | ||
if (!itemStr) { | ||
return null | ||
} | ||
|
@@ -103,32 +164,43 @@ humhub.module('firebase', function (module, require, $) { | |
return item.value; | ||
}; | ||
|
||
/** | ||
* Gets the URL for updating the token on the server. | ||
* @function | ||
* @returns {string} The token update URL. | ||
*/ | ||
const tokenUpdateUrl = function () { | ||
return module.config.tokenUpdateUrl; | ||
}; | ||
|
||
/** | ||
* Gets the sender ID from the module configuration. | ||
* @function | ||
* @returns {string} The sender ID. | ||
*/ | ||
const senderId = function () { | ||
return module.config.senderId; | ||
}; | ||
|
||
module.export({ | ||
init: init, | ||
|
||
isTokenSentToServer: isTokenSentToServer, | ||
sendTokenToServer: sendTokenToServer, | ||
afterServiceWorkerRegistration: afterServiceWorkerRegistration, | ||
|
||
// Config Vars | ||
senderId: senderId, | ||
tokenUpdateUrl: tokenUpdateUrl, | ||
|
||
// LocalStore Helper | ||
setTokenLocalStore: setTokenLocalStore, | ||
getTokenLocalStore: getTokenLocalStore, | ||
deleteTokenLocalStore: deleteTokenLocalStore, | ||
addPushNotificationPermissionsInfo: addPushNotificationPermissionsInfo | ||
}); | ||
}); | ||
|
||
/** | ||
* Handles actions after the service worker registration (global scope). | ||
* @function | ||
* @param {ServiceWorkerRegistration} registration - The service worker registration object. | ||
*/ | ||
function afterServiceWorkerRegistration(registration) { | ||
humhub.modules.firebase.afterServiceWorkerRegistration(registration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
<?php | ||
|
||
namespace humhub\modules\fcmPush\widgets; | ||
class PushNotificationInfoWidget extends \humhub\components\Widget | ||
{ | ||
|
||
use Yii; | ||
use yii\helpers\Url; | ||
use humhub\components\Widget; | ||
use humhub\modules\fcmPush\Module; | ||
use humhub\modules\fcmPush\services\DriverService; | ||
|
||
class PushNotificationInfoWidget extends Widget | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function run() | ||
{ | ||
//return $this->render('push-notification-info'); | ||
} | ||
/** @var Module $module */ | ||
$module = Yii::$app->getModule('fcm-push'); | ||
$pushDriver = (new DriverService($module->getConfigureForm()))->getWebDriver(); | ||
|
||
if ($pushDriver === null) { | ||
return ''; | ||
} | ||
|
||
} | ||
return $this->render('push-notification-info', [ | ||
'tokenUpdateUrl' => Url::to(['/fcm-push/token/update']), | ||
'senderId' => $pushDriver->getSenderId(), | ||
]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter here isn't also not longer used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also not sure if we need these calls
addPushNotificationPermissionsInfo
in theafterServiceWorkerRegistration
at all.Because usually the ServiceWorkerRegistration takes place very early, e.g. at login, and the PermissionInfo is not displayed anywhere here. In addition, the registration is only once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have to look into this more when I have the time to do so. 🤔