Sending Notifications

There are two ways to send notifications in Megaphone — using the NotificationService or using delegates (requires ColdBox 7+).

NotificationService

Notifications are sent using the NotificationService, often aliased as megaphone.

// handlers/StockRebalancing.cfc
component {

    property name="megaphone" inject="NotificationService@megaphone";

    function create( event, rc, prc ) {
        // ...
        var notification = getInstance( "StockRebalancingCompleteNotification" )
        notification.setStockSymbol( "APPL" );
        notification.setCompletionTimestamp( now() );
        megaphone.notify( auth().user(), notification );
        // ...
    }

}

For those of you allergic to calling getInstance (😜), you can also pass a string name and a struct of properties:

SendsNotifications Delegate

Another way to send a Notification is by adding the SendsNotifications delegate to a Notifiable instance.

Then you can call a notify method on the Notifiable instance.

The notify method from the delegate can be passed either a Notification instance or a string name just like the notify method on the NotificationService.

Last updated