0

I am currently trying to implement what should be a simple task and send a message to one and / or more users via Moodle. In the process I found 2 guides, which confused me at first. Messaging 2.0 and Message API. Both have some differences and I didn't understand if my plugin needs to register as a Message Producer or not.

Unfortunately, I just can't get my message to be sent. I logged in with the user and checked the mailbox, no message there. Any help is appreciated.

What I did: Created message.php within my db - folder and inserted the following code:

<?php
defined('MOODLE_INTERNAL') || die();

$messageproviders = array (
    'datenotification' => array (
    )
);

After that I expanded my language file with this:

$string['messageprovider:datenotification'] = 'Reminder for a presentation';

Like the guide advised, I updated my plugin to insert my messageprovider in the table mdl_message_providers. Finally, I implemented the actual sending of the message.

$eventdata = new \core\message\message();
$eventdata->component         = 'local_reminder';    // the component sending the message. Along with name this must exist in the table message_providers
$eventdata->name              = 'datenotification';        // type of message from that module (as module defines it). Along with component this must exist in the table message_providers
$eventdata->userfrom          = core_user::get_noreply_user();      // user object , no-reply
$eventdata->userto            = $user;        // user object from database
$eventdata->subject           = 'Test message';   
$eventdata->fullmessage       = 'This is my test message';      
$eventdata->fullmessageformat = FORMAT_PLAIN;   // text format
$eventdata->fullmessagehtml   = '<p>This is my test message</p>';      
$eventdata->smallmessage      = '';            
$eventdata->courseid = $course_id; // This is required in recent versions, use it from 3.2 on https://tracker.moodle.org/browse/MDL-47162

$result = message_send($eventdata);

Debugging:

var_dump($eventdata); //-> All data is included                                              
var_dump($result); // returns int(5), id of message, no error
Scorch
  • 153
  • 2
  • 17
  • 1
    Check if the message is installed and enabled via site admin > messaging > notification settings or direct to yoursite//admin/message.php – Russell England Aug 31 '22 at 06:44
  • 1
    Also check the user preferences at /message/notificationpreferences.php?userid=x - replace x with the user id – Russell England Aug 31 '22 at 06:46
  • I turned on the web option for my plugin at /admin/message.php and it works like a charm, thanks for pointing that out. – Scorch Aug 31 '22 at 14:08

1 Answers1

1

Adding the comment as an answer for future reference

Check if the message is installed and enabled via site admin > messaging > notification settings or direct to yoursite/admin/message.php

Russell England
  • 9,436
  • 1
  • 27
  • 41