WoltLab Suite 6.2 Handbuch
Deutsch/PHP-Version: Die Website-Struktur, Navigation und Überschriften sind auf Deutsch vorbereitet. Code-Beispiele und technische Namen bleiben unverändert.

User Notifications

WoltLab Suite includes a powerful user notification system that supports notifications directly shown on the website and emails sent immediately or on a daily basis.

objectType.xml

For any type of object related to events, you have to define an object type for the object type definition com.woltlab.wcf.notification.objectType:

{jinja{ codebox(

title="objectType.xml",

language="xml",

filepath="php/api/user_notifications/objectType.xml"

) }}

The referenced class FooUserNotificationObjectType has to implement the IUserNotificationObjectType interface, which should be done by extending AbstractUserNotificationObjectType.

{jinja{ codebox(

title="files/lib/system/user/notification/object/type/FooUserNotificationObjectType.class.php",

language="php",

filepath="php/api/user_notifications/FooUserNotificationObjectType.class.php"

) }}

You have to set the class names of the database object ($objectClassName) and the related list ($objectListClassName).

Additionally, you have to create a class that implements the IUserNotificationObject whose name you have to set as the value of the $decoratorClassName property.

{jinja{ codebox(

title="files/lib/system/user/notification/object/FooUserNotificationObject.class.php",

language="php",

filepath="php/api/user_notifications/FooUserNotificationObject.class.php"

) }}

userNotificationEvent.xml

Each event that you fire in your package needs to be registered using the user notification event package installation plugin.

An example file might look like this:

{jinja{ codebox(

title="userNotificationEvent.xml",

language="xml",

filepath="php/api/user_notifications/userNotificationEvent.xml"

) }}

Here, you reference the user notification object type created via objectType.xml.

The referenced class in the <classname> element has to implement the IUserNotificationEvent interface by extending the AbstractUserNotificationEvent class or the AbstractSharedUserNotificationEvent class if you want to pre-load additional data before processing notifications.

In AbstractSharedUserNotificationEvent::prepare(), you can, for example, tell runtime caches to prepare to load certain objects which then are loaded all at once when the objects are needed.

{jinja{ codebox(

title="files/lib/system/user/notification/event/FooUserNotificationEvent.class.php",

language="php",

filepath="php/api/user_notifications/FooUserNotificationEvent.class.php"

) }}

For daily emails ($notificationType = 'daily'), only application, template, and variables are supported.

Firing Events

When the action related to a user notification is executed, you can use UserNotificationHandler::fireEvent() to create the notifications:

$recipientIDs = []; // fill with user ids of the recipients of the notification
UserNotificationHandler::getInstance()->fireEvent(
	'bar', // event name
	'com.woltlab.example.foo', // event object type name
	new FooUserNotificationObject(new Foo($fooID)), // object related to the event
	$recipientIDs
);

Marking Notifications as Confirmed

In some instances, you might want to manually mark user notifications as confirmed without the user manually confirming them, for example when they visit the page that is related to the user notification.

In this case, you can use UserNotificationHandler::markAsConfirmed():

$recipientIDs = []; // fill with user ids of the recipients of the notification
$fooIDs = []; // fill with ids of related foo objects
UserNotificationHandler::getInstance()->markAsConfirmed(
	'bar', // event name
	'com.woltlab.example.foo', // event object type name
	$recipientIDs,
	$fooIDs
);