from odoo import models, fields, api from odoo.exceptions import UserError class PushNotificationWizard(models.TransientModel): _name = 'mapan.push.wizard' _description = 'Send Mobile App Notification' title = fields.Char(string='Notification Title', required=True) body = fields.Text(string='Notification Body', required=True) recipient_type = fields.Selection([ ('all', 'All Activated Members'), ('specific', 'Specific Members') ], string='Send To', default='all', required=True) partner_ids = fields.Many2many( 'res.partner', string='Recipients', domain=lambda self: [('user_ids.groups_id', 'in', [self.env.ref('base.group_portal').id]), ('user_ids.active', '=', True)] ) def action_send_push(self): # Determine target partners based on recipient type partners = self.partner_ids if self.recipient_type == 'specific' else self.env['res.partner'] # Create the notification record in the DB instead of calling Firebase self.env['mapan.app.notification'].create({ 'title': self.title, 'body': self.body, 'partner_ids': [(6, 0, partners.ids)] }) recipient_count = len(partners) target_msg = f"{recipient_count} selected partners" if self.recipient_type == 'specific' else "all app users" return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': 'App Notification Dispatched', 'message': f'Message successfully staged for {target_msg}!', 'type': 'success', 'sticky': False, } }