From 812006061fec95089e7852f681915e8460ffe0ff Mon Sep 17 00:00:00 2001 From: eslamabady Date: Tue, 24 Sep 2024 11:26:25 +0300 Subject: [PATCH] mobile notification --- odex25_mobile/odex_mobile/i18n/ar_001.po | 5 ++ .../models/attendence_zone_config.py | 2 + .../odex_mobile/models/hr_employee.py | 62 +++++++++++++++---- .../views/attendance_zone_config_view.xml | 11 +++- .../views/attendance_zone_config_view.xml | 2 +- 5 files changed, 68 insertions(+), 14 deletions(-) diff --git a/odex25_mobile/odex_mobile/i18n/ar_001.po b/odex25_mobile/odex_mobile/i18n/ar_001.po index e45caa4d2..f366b28d4 100644 --- a/odex25_mobile/odex_mobile/i18n/ar_001.po +++ b/odex25_mobile/odex_mobile/i18n/ar_001.po @@ -32,6 +32,11 @@ msgstr " تسجيل الدخول" msgid " Sign-out" msgstr " تسجيل الخروج" +#. module: odex_mobile +#: model_terms:ir.ui.view,arch_db:odex_mobile.res_config_settings_view_form +msgid "Odex SS App" +msgstr "تطبيق أودكس SS" + #. module: odex_mobile #: model_terms:ir.ui.view,arch_db:odex_mobile.privacy_policy msgid "Changes to This Privacy Policy" diff --git a/odex25_mobile/odex_mobile/models/attendence_zone_config.py b/odex25_mobile/odex_mobile/models/attendence_zone_config.py index eeddb14ec..cd49a0332 100644 --- a/odex25_mobile/odex_mobile/models/attendence_zone_config.py +++ b/odex25_mobile/odex_mobile/models/attendence_zone_config.py @@ -62,6 +62,7 @@ class ResConfigSettings(models.TransientModel): auto_checkout = fields.Integer(related="company_id.auto_checkout" ,string="Auto Checkout After",readonly=False) fcm_server_key = fields.Char(string='Server Key:', related="company_id.fcm_server_key", readonly=False) + service_account = fields.Binary(string='Service Account:', related="company_id.service_account", readonly=False) sender_id = fields.Char(string='Sender ID:', related="company_id.sender_id", readonly=False) class ResCompany(models.Model): @@ -69,5 +70,6 @@ class ResCompany(models.Model): auto_checkout = fields.Integer(string="Auto Checkout After") fcm_server_key = fields.Char(string='Server Key') + service_account = fields.Binary(string='Service Account', attachment=True) sender_id = fields.Char(string='Sender ID') diff --git a/odex25_mobile/odex_mobile/models/hr_employee.py b/odex25_mobile/odex_mobile/models/hr_employee.py index bfb7ec31e..a84828b38 100644 --- a/odex25_mobile/odex_mobile/models/hr_employee.py +++ b/odex25_mobile/odex_mobile/models/hr_employee.py @@ -5,6 +5,14 @@ from odoo.exceptions import ValidationError import random import json import json, requests +import json, requests, base64 +import google.auth.transport.requests +from google.oauth2 import service_account + +BASE_URL = 'https://fcm.googleapis.com' +SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'] +import tempfile +import os class HrEmployee(models.Model): @@ -15,18 +23,50 @@ class HrEmployee(models.Model): attendance_log_ids = fields.One2many('attendance.log','employee_id',string="Attendance Log") message_sent = fields.Boolean(string="Message Sent", default=False) def user_push_notification(self, notification): - url = "https://fcm.googleapis.com/fcm/send" - header = { - 'Content-Type': 'application/json', - 'Authorization': 'key=%s' % (self.env.user.company_id.fcm_server_key) - } - body = json.dumps({ - "to": self.fcm_token, - "direct_boot_ok": True, - "notification": notification - }) + def _get_access_token(json_file): + """Retrieve a valid access token that can be used to authorize requests. + + :return: Access token. + """ + credentials = service_account.Credentials.from_service_account_file( + json_file, scopes=SCOPES) + request = google.auth.transport.requests.Request() + credentials.refresh(request) + return credentials.token + try: - respons = requests.post(url=url, data=body, headers=header) + json_file_name = 'service-account' + base64_service_account = base64.decodebytes(self.env.user.company_id.service_account) + service_account_json = json.loads(base64_service_account) + with tempfile.TemporaryDirectory() as temp_dir: + temp_file_path = os.path.join(temp_dir, json_file_name) + with open(temp_file_path, 'w') as temp_file: + temp_file.write(base64_service_account.decode('UTF-8')) + header = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer %s' % (_get_access_token(temp_file_path)) + } + body = json.dumps({ + "message": { + "token": self.fcm_token_web, + "notification": notification, + "android": { + "notification": { + "sound": "default" + } + }, + "apns": { + "payload": { + "aps": { + "sound": "default" + } + } + } + } + }) + FCM_ENDPOINT = 'v1/projects/' + service_account_json['project_id'] + '/messages:send' + FCM_URL = BASE_URL + '/' + FCM_ENDPOINT + respons = requests.post(url=FCM_URL, data=body, headers=header) return True except Exception as e: return False diff --git a/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml b/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml index 4b41912a7..91dd23fd8 100644 --- a/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml +++ b/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml @@ -81,10 +81,17 @@ -
-

Mobile Attendance

+
+

Odex SS App

+
+
+
+
+