跳至内容
菜单
此问题已终结
2 回复
281 查看

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.


形象
丢弃
编写者

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

最佳答案

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.

形象
丢弃
编写者 最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
0
9月 25
263
2
9月 25
171
2
4月 25
3400
1
12月 24
1548
0
10月 24
2053