Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
348 Zobrazení

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.


Avatar
Zrušit
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

Nejlepší odpověď

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.

Avatar
Zrušit
Autor Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
0
zář 25
318
2
zář 25
263
2
dub 25
3460
1
pro 24
1600
0
říj 24
2080