Skip to Content
Menu
This question has been flagged
3 Replies
138 Zobrazenia

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šiť
Autor Best Answer

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šiť
Best Answer

Hello OdooIntern,



  To send custom notifications to specific users in Odoo 17 EE, you might need to take a slightly different approach since the direct mail channel functionality you're referring to has evolved. Here's a general guide to achieve your goal using automated actions and email templates:

  1. Create an Email Template under the Email module. Customize the template to include your sticky note and body text.

  2. Go to Settings > Technical > Automation > Automated Actions. Set up a new action specifying the trigger conditions.

  3. In the action's settings, choose 'Send Email' and select the template you created. Define the recipients based on your specific user criteria.


For personalized assistance:
https://www.pragtech.co.in/contact-us-mql.html

Avatar
Zrušiť
Best Answer

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šiť
Related Posts Replies Zobrazenia Aktivita
0
sep 25
182
2
apr 25
3362
1
dec 24
1515
0
okt 24
2001
2
aug 24
4312