53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
import json
|
|
import requests
|
|
import logging
|
|
|
|
from odoo import models, fields, api
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class Partner(models.Model):
|
|
_name = 'res.partner'
|
|
_inherit = ['res.partner', 'push.notification.mixin']
|
|
|
|
firebase_registration_ids = fields.One2many(
|
|
"firebase.registration", "partner_id", readonly=True
|
|
)
|
|
|
|
def send_notification(self, message_title, message_body, data=None, all_device=True):
|
|
notification_data = {
|
|
"title": str(message_title),
|
|
"body": str(message_body),
|
|
"meta": json.dumps(data) if data else None,
|
|
"partner_ids": [(4, self.id)],
|
|
"is_system": True,
|
|
"sent": True,
|
|
}
|
|
notification = self.env['firebase.notification'].sudo().create(notification_data)
|
|
|
|
if all_device:
|
|
emp = self.env['hr.employee'].sudo().search([('user_id', 'in', notification.partner_ids.user_ids.ids)])
|
|
if emp.user_id.partner_id:
|
|
emp.send_push_notification(str(message_title), str(message_body))
|
|
for reg in self.firebase_registration_ids:
|
|
reg.with_context(lang=self.lang).send_message(
|
|
message_title,
|
|
message_body,
|
|
data={
|
|
"title": str(message_title),
|
|
"body": str(message_body),
|
|
"meta": json.dumps(data) if data else None,
|
|
"is_system": "true",
|
|
'viewed': "false",
|
|
"sent": "true",
|
|
"data": str(notification.create_date),
|
|
"id": str(notification.id)
|
|
}
|
|
)
|
|
else:
|
|
if self.firebase_registration_ids:
|
|
self.firebase_registration_ids[0].with_context(lang=self.lang).send_message(
|
|
message_title,
|
|
message_body,
|
|
data=data
|
|
)
|