@@ -25,7 +25,7 @@ $declineAction = $notification->createAction();
2525$declineAction->setLabel('decline')
2626 ->setLink('/apps/files_sharing/api/v1/remote_shares/1337', 'DELETE');
2727
28- $notification->setApp('files_sharing ')
28+ $notification->setApp('myapp ')
2929 ->setUser('recipient1')
3030 ->setDateTime(new DateTime())
3131 ->setObject('remote', '1337') // $type and $id
@@ -47,70 +47,79 @@ $manager->notify($notification);
4747### Preparing a notification for display
4848
4949 1 . In ` app.php ` register your Notifier (` \OCP\Notification\INotifier ` ) interface to the manager,
50- using a ` \Closure ` :
50+ using a ` \Closure ` returning the Notifier and a ` \Closure ` returning an array of the id and name :
5151``` php
5252$manager = \OC::$server->getNotificationManager();
5353$manager->registerNotifier(function() {
5454 return new \OCA\Files_Sharing\Notifier(
5555 \OC::$server->getL10NFactory()
5656 );
57+ }, function() {
58+ $l = \OC::$server->getL10N('myapp');
59+ return [
60+ 'id' => 'myapp',
61+ 'name' => $l->t('My apps name'),
62+ ];
5763});
5864```
5965
6066 2 . The manager will execute the closure and then call the ` prepare() ` method on your notifier.
6167 If the notification is not known by your app, just throw an ` \InvalidArgumentException ` ,
6268 but if it is actually from your app, you must set the parsed subject, message and action labels:
6369``` php
64- protected $factory;
70+ class Notifier implements OCP\Notification\INotifier {
6571
66- public function __construct(\OCP\L10N\IFactory $factory) {
67- $this->factory = $factory;
68- }
72+ protected $factory;
6973
70- /**
71- * @param INotification $notification
72- * @param string $languageCode The code of the language that should be used to prepare the notification
73- */
74- public function prepare(INotification $notification, $languageCode) {
75- if ($notification->getApp() !== 'files_sharing') {
76- // Not my app => throw
77- throw new \InvalidArgumentException();
74+ public function __construct(\OCP\L10N\IFactory $factory) {
75+ $this->factory = $factory;
7876 }
7977
80- // Read the language from the notification
81- $l = $this->factory->get('myapp', $languageCode);
82-
83- switch ($notification->getSubject()) {
84- // Deal with known subjects
85- case 'remote_share':
86- $notification->setParsedSubject(
87- (string) $l->t('You received the remote share "%s"', $notification->getSubjectParameters())
88- );
89-
90- // Deal with the actions for a known subject
91- foreach ($notification->getActions() as $action) {
92- switch ($action->getLabel()) {
93- case 'accept':
94- $action->setParsedLabel(
95- (string) $l->t('Accept')
96- );
97- break;
98-
99- case 'decline':
100- $action->setParsedLabel(
101- (string) $l->t('Decline')
102- );
103- break;
78+ /**
79+ * @param INotification $notification
80+ * @param string $languageCode The code of the language that should be used to prepare the notification
81+ */
82+ public function prepare(INotification $notification, $languageCode) {
83+ if ($notification->getApp() !== 'myapp') {
84+ // Not my app => throw
85+ throw new \InvalidArgumentException();
86+ }
87+
88+ // Read the language from the notification
89+ $l = $this->factory->get('myapp', $languageCode);
90+
91+ switch ($notification->getSubject()) {
92+ // Deal with known subjects
93+ case 'remote_share':
94+ $notification->setParsedSubject(
95+ (string) $l->t('You received the remote share "%s"', $notification->getSubjectParameters())
96+ );
97+
98+ // Deal with the actions for a known subject
99+ foreach ($notification->getActions() as $action) {
100+ switch ($action->getLabel()) {
101+ case 'accept':
102+ $action->setParsedLabel(
103+ (string) $l->t('Accept')
104+ );
105+ break;
106+
107+ case 'decline':
108+ $action->setParsedLabel(
109+ (string) $l->t('Decline')
110+ );
111+ break;
112+ }
113+
114+ $notification->addParsedAction($action);
104115 }
116+ return $notification;
117+ break;
105118
106- $notification->addParsedAction($action);
107- }
108- return $notification;
109- break;
110-
111- default:
112- // Unknown subject => Unknown notification => throw
113- throw new \InvalidArgumentException();
119+ default:
120+ // Unknown subject => Unknown notification => throw
121+ throw new \InvalidArgumentException();
122+ }
114123 }
115124}
116125```
@@ -126,7 +135,7 @@ notification object:
126135
127136``` php
128137$manager = \OC::$server->getNotificationManager();
129- $notification->setApp('files_sharing ')
138+ $notification->setApp('myapp ')
130139 ->setObject('remote', 1337)
131140 ->setUser('recipient1');
132141$manager->markProcessed($notification);
@@ -138,7 +147,7 @@ remove all notifications for the app files_sharing on the object "remote #1337":
138147
139148``` php
140149$manager = \OC::$server->getNotificationManager();
141- $notification->setApp('files_sharing ')
150+ $notification->setApp('myapp ')
142151 ->setObject('remote', 1337);
143152$manager->markProcessed($notification);
144153```
0 commit comments