Se rendre au contenu
Menu
Cette question a été signalée
2 Réponses
321 Vues

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
Ignorer
Auteur

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

Meilleure réponse

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
Ignorer
Auteur Meilleure réponse

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
Ignorer
Publications associées Réponses Vues Activité
0
sept. 25
289
2
sept. 25
237
2
avr. 25
3443
1
déc. 24
1586
0
oct. 24
2065