Beginner can't get notification API to work

I’m writing my first app and trying to send a notification by following this guide.

The code is mostly copied from the guide but when I execute it I don’t see any notification. No exceptions are thrown when it runs. Any advice on why this code isn’t producing a notification?

		$manager = \OC::$server->getNotificationManager();
		$notification = $manager->createNotification();

		$notification->setApp('batchit') // My app ID
		    ->setUser('ryan')
		    ->setDateTime(new \DateTime())
		    ->setObject('remote', '1337') // What are these for?
		    ->setSubject('This is the notification subject.')
		;

		$manager->notify($notification);

The code is mostly copied from the guide but when I execute it I don’t see any notification.

Can you check if the notification ends up in the oc_notifications table?

->setObject('remote', '1337') // What are these for?

Notifications are always about objects. E.g. a file, chat message, etc. setObject() has 2 arguments: 1. type (remote in your sample) and 2. id (1337 in your sample) to relate to the object. You can then use this information when rendering the notification again to e.g. take the file name at the moment of showing instead of sharing (in case it was renamed inbetween).

What you are mostlikely missing is the rendering part:

Now I just noticed that the docs are quite outdated there.

See this working sample instead: Codeblock of 1.

And this is the notifier:

1 Like

I misunderstood and thought the rendering part was done by whichever application is displaying it, such as the core server or a client like the mobile app. Now I get it. Thanks!

1 Like

Updated docs in Update documentation to the new way by nickvergessen · Pull Request #891 · nextcloud/notifications · GitHub

1 Like