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:
// handlers/StockRebalancing.cfc
component {
property name="megaphone" inject="NotificationService@megaphone";
function create( event, rc, prc ) {
// ...
megaphone.notify(
auth().user(),
"StockRebalancingCompleteNotification",
{ "stockSymbol": "APPL", "completionTimestamp": now() }
);
// ...
}
}
SendsNotifications Delegate
Another way to send a Notification is by adding the SendsNotifications
delegate to a Notifiable
instance.
component name="User" delegates="SendsNotifications@megaphone" accessors="true" {
property name="id";
public string function getNotifiableId() {
return getId();
}
public string function getNotifiableType() {
return "User";
}
}
Then you can call a notify
method on the Notifiable
instance.
// handlers/StockRebalancing.cfc
component {
property name="megaphone" inject="NotificationService@megaphone";
function create( event, rc, prc ) {
// ...
auth().user().notify(
"StockRebalancingCompleteNotification",
{ "stockSymbol": "APPL", "completionTimestamp": now() }
);
// ...
}
}
Last updated