Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
280 Widoki

I want to send custom notification with sticky and body to a certain users.

I wanted to do this like creating mail channel and then sending it but it seems on odoo17 EE it does not exist please help.


Awatar
Odrzuć
Autor

what cybrosis provided works in odoo that uses venv so this is closest answer as i have not enough karma points please close it

Najlepsza odpowiedź

Hi,

In Odoo 17, the way notifications work has changed compared to older versions. The classic “mail channel → message post” approach you may know isn’t always needed for user notifications. You can send custom sticky notifications directly to specific users using the bus/notifications framework.


Code:

from odoo import models, api

from odoo.http import request


class MyModel(models.Model):

    _name = "my.model"


    @api.model

    def send_custom_notification(self, user_ids, title, body):

        for user_id in user_ids:

            request.env['bus.bus'].sendone(

                (f"user.notifications", user_id),

                {

                    'sticky': True,

                    'title': title,

                    'message': body,

                    'type': 'notification'

                }

            )


You can call this from a server action or from your Python code whenever a certain event occurs.


Hope it helps.

Awatar
Odrzuć
Autor Najlepsza odpowiedź

i am using docker and i currently have this code def trigger_test_notif(self):

self.ensure_one()

user_ids = [2, 6]

current_user_id = self.env.uid


message_body = _("This is a test message to another user")


_logger.info("Triggering notification for users: %s", user_ids)


for uid in user_ids:

user = self.env['res.users'].sudo().browse(uid)

if not user.exists():

_logger.warning("User with ID %s does not exist", uid)

continue

_logger.info("Sending bus notification to user_id=%s", uid)

self.env['bus.bus'].sudo()._sendone(

user.partner_id.id,

"display_notification",

{

'title': _("Test Notification"),

'message': message_body,

'sticky': True,

'type': "notification",

}

)


for uid in user_ids:

user = self.env['res.users'].sudo().browse(uid)

if not user.exists() or not user.partner_id:

_logger.warning("User with ID %s has no valid partner", uid)

continue

partner_id = user.partner_id.id

_logger.info("Creating persistent mail.message for partner_id=%s", partner_id)

haa = request.env['mail.message'].sudo().create({

'message_type': 'notification',

'subtype_id': self.env.ref('mail.mt_note').id,

'subject': _("Test Notification"),

'body': message_body,

'res_id': self.id,

'model': self._name,

'partner_ids': [(4, partner_id)],

})


if current_user_id not in user_ids:

user_ids.append(current_user_id)


return haa

this creates the object for mail.message but i am not receiving it on the test account

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
wrz 25
263
2
wrz 25
168
2
kwi 25
3400
1
gru 24
1547
0
paź 24
2053