pwaNotifications()
: Notification authorization requestThe pwaNotifications()
function offers the user a button to authorize the sending of notifications.
In order for the function to be functional, these elements must be added to the DOM:
pwa-permission-notifications-btn
: PWA notifications permission request buttonpwa-permission-remove
: PWA notification permission revocation explanation insert
<button class="pwa-permission-notifications-btn">
{...}
</button>
<div class="pwa-permission-remove">
To remove permission, please go to your device or browser settings
</div>
We hide the elements
.pwa-permission-notifications-btn,
.pwa-permission-remove {
display: none;
}
If the elements have the class is-visible
, we display them
.pwa-permission-notifications-btn.is-visible,
.pwa-permission-remove.is-visible {
display: block;
}
const pwaNotifications = async() => {
...
}
We set the DOM elements to be manipulated as variables
const pwaPermissionNotificationsBtn = document.querySelector('.pwa-permission-notifications-btn')
const pwaPermissionRemove = document.querySelector('.pwa-permission-remove')
If the user has accepted the sending of notifications, we add the class is-active
to the button
if (Notification. permission === 'granted') {
pwaPermissionNotificationsBtn.classList.add('is-active')
}
Otherwise we remove the is-active
class from the button
else {
pwaPermissionNotificationsBtn.classList.remove('is-active')
}
const pwaNotificationsPermission = async () => {
If the user has not refused the sending of notifications
if (Notification. permission !== 'denied') {
The user is asked for permission
const permission = await Notification.requestPermission()
If the user accepts
if(permission == "granted"){
We add the is-active
class to the button
pwaPermissionNotificationsBtn.classList.add('is-active')
pwaPermissionNotificationsBtn.addEventListener('click', async () => {
If the user has already accepted the sending of notifications
if(pwaPermissionNotificationsBtn.classList.contains('is-active')) {
Display the permission revocation explanation insert
pwaPermissionRemove.classList.add('is-visible')
Otherwise, we ask for permission
pwaNotificationsPermission()
If you click on the insert
pwaPermissionRemove.addEventListener('click', () => {
Hide permission revocation explanation insert
pwaPermissionRemove.classList.remove('is-visible')
pwaNotifications()
On pwabunga.js, the pwaNotifications()
function is called in the pwaParams()
function
pwaNotifications()
function
const pwaNotifications = async () => {
const pwaPermissionNotificationsBtn = document.querySelector('.pwa-permission-notifications-btn')
const pwaPermissionRemove = document.querySelector('.pwa-permission-remove')
if (Notification.permission === 'granted') {
pwaPermissionNotificationsBtn.classList.add('is-active')
} else {
pwaPermissionNotificationsBtn.classList.remove('is-active')
}
const pwaNotificationsPermission = async () => {
if (Notification.permission !== 'denied') {
const permission = await Notification.requestPermission()
if(permission == "granted"){
pwaPermissionNotificationsBtn.classList.add('is-active')
}
}
}
pwaPermissionNotificationsBtn.addEventListener('click', async () => {
if(pwaPermissionNotificationsBtn.classList.contains('is-active')) {
pwaPermissionRemove.classList.add('is-visible')
} else {
pwaNotificationsPermission()
}
})
pwaPermissionRemove.addEventListener('click', () => {
pwaPermissionRemove.classList.remove('is-visible')
})
}
pwaInstall()
: Promotion and PWA install buttonpwaUpdate()
: Application updatepwaShare()
: Using device native sharingpwaParams()
: PWA parameters insert