@ -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 **/ |
|||
import { jsonrpc } from "@web/core/network/rpc_service"; |
|||
/** |
|||
* Odoo module for handling Firebase push notifications. |
|||
* |
|||
* This module initializes Firebase messaging and handles push notifications |
|||
* for the current session's company. It also registers the service worker for |
|||
* handling notifications if the company has push notifications enabled. |
|||
* |
|||
* @module mail_push_notification |
|||
*/ |
|||
var vapid = ''; |
|||
var firebaseConfig = {}; |
|||
var push_notification = false; |
|||
/** |
|||
* Sends an RPC query to retrieve push notification settings for the current company. |
|||
* |
|||
* @function |
|||
* @returns {Promise} A promise that resolves with the push notification settings. |
|||
*/ |
|||
|
|||
// Initialize variables
|
|||
let vapid = ''; |
|||
let firebaseConfig = {}; |
|||
let messaging = null; |
|||
let push_notification = false; |
|||
|
|||
// Fetch push notification settings for the current company
|
|||
jsonrpc("/firebase_credentials", {}).then(function(data) { |
|||
if (data) { |
|||
if (data.push_notification) { |
|||
if (data && data.push_notification) { |
|||
push_notification = true; |
|||
if ("serviceWorker" in navigator) { |
|||
navigator.serviceWorker.register("/firebase-messaging-sw.js").then(function () {}); |
|||
} |
|||
navigator.serviceWorker.register("/firebase-messaging-sw.js").then(function () { |
|||
console.log('Service worker registered successfully.'); |
|||
}).catch(function (err) { |
|||
console.error('Failed to register service worker:', err); |
|||
}); |
|||
} |
|||
} |
|||
}); |
|||
// Fetch Firebase configuration details
|
|||
jsonrpc("/firebase_config_details", {}).then(function(data) { |
|||
if (data) { |
|||
var json = JSON.parse(data); |
|||
const json = JSON.parse(data); |
|||
vapid = json.vapid; |
|||
firebaseConfig = json.config; |
|||
/** |
|||
* Initializes Firebase messaging and sets up event listeners for incoming messages. |
|||
* |
|||
* @function |
|||
*/ |
|||
// Initialize Firebase app with the retrieved configuration
|
|||
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 |
|||
*/ |
|||
messaging.requestPermission().then(function () { |
|||
/** |
|||
* Retrieves the registration token and sends it to the server for subscription. |
|||
* |
|||
* @function |
|||
* @param {string} vapidKey - The VAPID key for authentication. |
|||
*/ |
|||
messaging = firebase.messaging(); |
|||
// Function to request notification permission and retrieve token
|
|||
function requestPermissionAndRetrieveToken() { |
|||
console.log('not',Notification) |
|||
Notification.requestPermission().then((permission) => { |
|||
console.log('permission',permission) |
|||
if (permission === 'granted') { |
|||
console.log('Permission granted'); |
|||
// Retrieve registration token
|
|||
messaging.getToken({ vapidKey: vapid }).then((currentToken) => { |
|||
console.log('Current token:', currentToken); |
|||
if (currentToken) { |
|||
/** |
|||
* Sends a POST request to the server with the registration token. |
|||
* |
|||
* @function |
|||
* @param {string} token - The registration token. |
|||
*/ |
|||
$.post("/push_notification", { |
|||
name: currentToken |
|||
// Send the token to the server for subscription
|
|||
$.post("/push_notification", { name: currentToken }).done(function(response) { |
|||
console.log('Token sent to server:', response); |
|||
}).fail(function(error) { |
|||
console.error('Failed to send token to server:', error); |
|||
}); |
|||
} else { |
|||
console.log('No registration token found'); |
|||
console.warn('No registration token available'); |
|||
} |
|||
}).catch((err) => { |
|||
console.log('There is an error has occurred while attempting to retrieve the token.', 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(); |
|||
}); |
|||
} |
|||
}); |
|||
|