@ -0,0 +1,29 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
############################################################################# |
||||
|
# |
||||
|
# Cybrosys Technologies Pvt. Ltd. |
||||
|
# |
||||
|
# Copyright (C) 2023-TODAY Cybrosys Technologies(<https://www.cybrosys.com>) |
||||
|
# Author: Gokul PI (<https://www.cybrosys.com>) |
||||
|
# |
||||
|
# You can modify it under the terms of the GNU LESSER |
||||
|
# GENERAL PUBLIC LICENSE (LGPL v3), Version 3. |
||||
|
# |
||||
|
# This program is distributed in the hope that it will be useful, |
||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details. |
||||
|
# |
||||
|
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE |
||||
|
# (LGPL v3) along with this program. |
||||
|
# If not, see <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
############################################################################# |
||||
|
from odoo import models |
||||
|
|
||||
|
|
||||
|
class ResUsers(models.Model): |
||||
|
_inherit = 'res.users' |
||||
|
|
||||
|
def has_push_notification_permission(self): |
||||
|
return self.has_group('base.group_user') |
Before Width: | Height: | Size: 132 KiB After Width: | Height: | Size: 118 KiB |
Before Width: | Height: | Size: 124 KiB After Width: | Height: | Size: 127 KiB |
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 126 KiB |
Before Width: | Height: | Size: 136 KiB After Width: | Height: | Size: 196 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 220 KiB After Width: | Height: | Size: 217 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 72 KiB |
@ -1,89 +1,81 @@ |
|||||
/** @odoo-module **/ |
/** @odoo-module **/ |
||||
import { jsonrpc } from "@web/core/network/rpc_service"; |
import { jsonrpc } from "@web/core/network/rpc_service"; |
||||
/** |
|
||||
* Odoo module for handling Firebase push notifications. |
// Initialize variables
|
||||
* |
let vapid = ''; |
||||
* This module initializes Firebase messaging and handles push notifications |
let firebaseConfig = {}; |
||||
* for the current session's company. It also registers the service worker for |
let messaging = null; |
||||
* handling notifications if the company has push notifications enabled. |
let push_notification = false; |
||||
* |
|
||||
* @module mail_push_notification |
// Fetch push notification settings for the current company
|
||||
*/ |
jsonrpc("/firebase_credentials", {}).then(function(data) { |
||||
var vapid = ''; |
if (data && data.push_notification) { |
||||
var firebaseConfig = {}; |
push_notification = true; |
||||
var push_notification = false; |
if ("serviceWorker" in navigator) { |
||||
/** |
navigator.serviceWorker.register("/firebase-messaging-sw.js").then(function () { |
||||
* Sends an RPC query to retrieve push notification settings for the current company. |
console.log('Service worker registered successfully.'); |
||||
* |
}).catch(function (err) { |
||||
* @function |
console.error('Failed to register service worker:', err); |
||||
* @returns {Promise} A promise that resolves with the push notification settings. |
|
||||
*/ |
|
||||
jsonrpc("/firebase_credentials", {}).then(function(data) { |
|
||||
if (data) { |
|
||||
if (data.push_notification) { |
|
||||
push_notification = true; |
|
||||
if ("serviceWorker" in navigator) { |
|
||||
navigator.serviceWorker.register("/firebase-messaging-sw.js").then(function () {}); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
}); |
|
||||
jsonrpc("/firebase_config_details", {}).then(function (data) { |
|
||||
if (data) { |
|
||||
var json = JSON.parse(data); |
|
||||
vapid = json.vapid; |
|
||||
firebaseConfig = json.config; |
|
||||
/** |
|
||||
* Initializes Firebase messaging and sets up event listeners for incoming messages. |
|
||||
* |
|
||||
* @function |
|
||||
*/ |
|
||||
firebase.initializeApp(firebaseConfig); |
|
||||
const messaging = firebase.messaging(); |
|
||||
/** |
|
||||
* Handles incoming push notification messages. |
|
||||
* |
|
||||
* @function |
|
||||
* @param {Object} payload - The notification payload. |
|
||||
*/ |
|
||||
messaging.onMessage((payload) => { |
|
||||
const notificationOptions = { |
|
||||
body: payload.notification.body, |
|
||||
}; |
|
||||
let notification = payload.notification; |
|
||||
navigator.serviceWorker.getRegistrations().then((registration) => { |
|
||||
registration[0].showNotification(notification.title, notificationOptions); |
|
||||
}); |
|
||||
}); |
}); |
||||
/** |
} |
||||
* Requests permission for receiving push notifications and retrieves the registration token. |
} |
||||
* |
}); |
||||
* @function |
// Fetch Firebase configuration details
|
||||
*/ |
jsonrpc("/firebase_config_details", {}).then(function(data) { |
||||
messaging.requestPermission().then(function () { |
if (data) { |
||||
/** |
const json = JSON.parse(data); |
||||
* Retrieves the registration token and sends it to the server for subscription. |
vapid = json.vapid; |
||||
* |
firebaseConfig = json.config; |
||||
* @function |
// Initialize Firebase app with the retrieved configuration
|
||||
* @param {string} vapidKey - The VAPID key for authentication. |
firebase.initializeApp(firebaseConfig); |
||||
*/ |
messaging = firebase.messaging(); |
||||
messaging.getToken({ vapidKey: vapid }).then((currentToken) => { |
// Function to request notification permission and retrieve token
|
||||
if (currentToken) { |
function requestPermissionAndRetrieveToken() { |
||||
/** |
console.log('not',Notification) |
||||
* Sends a POST request to the server with the registration token. |
Notification.requestPermission().then((permission) => { |
||||
* |
console.log('permission',permission) |
||||
* @function |
if (permission === 'granted') { |
||||
* @param {string} token - The registration token. |
console.log('Permission granted'); |
||||
*/ |
// Retrieve registration token
|
||||
$.post("/push_notification", { |
messaging.getToken({ vapidKey: vapid }).then((currentToken) => { |
||||
name: currentToken |
console.log('Current token:', currentToken); |
||||
}); |
if (currentToken) { |
||||
} else { |
// Send the token to the server for subscription
|
||||
console.log('No registration token found'); |
$.post("/push_notification", { name: currentToken }).done(function(response) { |
||||
} |
console.log('Token sent to server:', response); |
||||
}).catch((err) => { |
}).fail(function(error) { |
||||
console.log('There is an error has occurred while attempting to retrieve the token.', err); |
console.error('Failed to send token to server:', error); |
||||
}); |
}); |
||||
|
} else { |
||||
|
console.warn('No registration token available'); |
||||
|
} |
||||
|
}).catch((err) => { |
||||
|
console.error('Error retrieving token:', err); |
||||
|
}); |
||||
|
} else { |
||||
|
console.warn('Permission for notifications was denied'); |
||||
|
} |
||||
|
}).catch((err) => { |
||||
|
console.error('Unable to get permission to notify:', err); |
||||
}); |
}); |
||||
} |
} |
||||
}); |
// Initialize Firebase messaging and handle incoming messages
|
||||
|
messaging.onMessage((payload) => { |
||||
|
// Show notification to user
|
||||
|
const notificationTitle = payload.notification.title; |
||||
|
const notificationOptions = { |
||||
|
body: payload.notification.body, |
||||
|
icon: payload.notification.icon |
||||
|
}; |
||||
|
navigator.serviceWorker.getRegistrations().then((registrations) => { |
||||
|
registrations[0].showNotification(notificationTitle, notificationOptions); |
||||
|
}).catch((err) => { |
||||
|
console.error('Error showing notification:', err); |
||||
|
}); |
||||
|
}); |
||||
|
// Request permission and retrieve token when the DOM content is loaded
|
||||
|
document.addEventListener('DOMContentLoaded', (event) => { |
||||
|
requestPermissionAndRetrieveToken(); |
||||
|
}); |
||||
|
} |
||||
|
}); |
||||
|