[UPD] odex_web_app: firebase changed documentation
This commit is contained in:
parent
3cdeff4af1
commit
e42a8799f8
|
|
@ -6,7 +6,9 @@ import datetime
|
|||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
web_fcm_server_key = fields.Char(string='Server Key:', related="company_id.web_fcm_server_key", readonly=False)
|
||||
service_account = fields.Binary(string='Service Account:', related="company_id.service_account", readonly=False)
|
||||
web_sender_id = fields.Char(string='Sender ID:', related="company_id.web_sender_id", readonly=False)
|
||||
|
||||
|
||||
|
|
@ -14,4 +16,5 @@ class ResCompany(models.Model):
|
|||
_inherit = 'res.company'
|
||||
|
||||
web_fcm_server_key = fields.Char(string='Server Key')
|
||||
service_account = fields.Binary(string='Service Account', attachment=True)
|
||||
web_sender_id = fields.Char(string='Sender ID')
|
||||
|
|
|
|||
|
|
@ -1,10 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime, timedelta
|
||||
from odoo import models,fields,api,_
|
||||
from odoo.exceptions import ValidationError
|
||||
import random
|
||||
import json
|
||||
import json, requests
|
||||
from odoo import models, fields
|
||||
import json, requests, base64
|
||||
import google.auth.transport.requests
|
||||
from google.oauth2 import service_account
|
||||
import io
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
|
||||
BASE_URL = 'https://fcm.googleapis.com'
|
||||
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']
|
||||
|
||||
|
||||
class HrEmployee(models.Model):
|
||||
|
|
@ -15,18 +20,38 @@ class HrEmployee(models.Model):
|
|||
|
||||
|
||||
def user_push_notification_web(self, notification):
|
||||
url = "https://fcm.googleapis.com/fcm/send"
|
||||
header = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'key=%s' % (self.env.user.company_id.web_fcm_server_key)
|
||||
}
|
||||
body = json.dumps({
|
||||
"to": self.fcm_token_web,
|
||||
"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({
|
||||
"to": self.fcm_token_web,
|
||||
"direct_boot_ok": True,
|
||||
"notification": notification
|
||||
})
|
||||
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
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ class MailThread(models.AbstractModel):
|
|||
for employee_id in self.env['hr.employee'].sudo().search([('user_id', 'in', partners_to_notify.user_ids.ids)]):
|
||||
push_notify = employee_id.user_push_notification_web({
|
||||
"title": self.display_name,
|
||||
"body": notification_body
|
||||
"body": notification_body,
|
||||
"sound": "default"
|
||||
})
|
||||
|
||||
return super(MailThread, self).message_post(message_type=message_type, **kwargs)
|
||||
|
|
@ -15,6 +15,12 @@
|
|||
<h2>WebMobile Configuration</h2>
|
||||
<div id="web_fcm_settings">
|
||||
<div class="row mt16 o_settings_container">
|
||||
<div class="col-xs-12 col-md-6 o_setting_box">
|
||||
<div class="o_setting_right_pane">
|
||||
<label for="service_account"/>
|
||||
<field name="service_account"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-xs-12 col-md-6 o_setting_box">
|
||||
<div class="o_setting_right_pane">
|
||||
<label for="web_fcm_server_key"/>
|
||||
|
|
|
|||
Loading…
Reference in New Issue