nextcloud / notifications

:bell: Notifications app for Nextcloud

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Do defered notifications still work?

StCyr opened this issue · comments

Steps to reproduce

Following code:

        // Defers sending notifications to avoid multiple connections to the server
        $shouldFlush = $this->notificationManager->defer();

        // Prepare notifications for all invalid users
        foreach ($inactiveUsers as $inactiveUser) {
            $this->logger->info('Sending notification to user ' . $inactiveUser->getUid());
            $notification = $this->notificationManager->createNotification();
            $notification->setApp('myapp')
                ->setUser($inactiveUser->getUid())
                ->setDateTime(new \DateTime())
                ->setObject('settings', 'admin')
                ->setSubject('Please activate myapp for your account');
        }

        // Sends notifications (if no other app is already deferring)
        if ($shouldFlush) {
            $this->notificationManager->flush();
        }

This is, afaict, exactly what the documentation says (Note that I stepped into the code and could see that $shouldFlush was true).

Expected behaviour

Send notifications

Actual behaviour

Nothing happens

Additional info

If I use regular (ie: non-defered) notifications, it works perfectly:

        // Defers sending notifications to avoid multiple connections to the server
        //$shouldFlush = $this->notificationManager->defer();

        // Prepare notifications for all invalid users
        foreach ($inactiveUsers as $inactiveUser) {
            $this->logger->info('Sending notification to user ' . $inactiveUser->getUid());
            $notification = $this->notificationManager->createNotification();
            $notification->setApp('sendentsynchroniser')
                ->setUser($inactiveUser->getUid())
                ->setDateTime(new \DateTime())
                ->setObject('settings', 'admin')
                ->setSubject('Please activate your Exchange synchronisation');
            $this->notificationManager->notify($notification);
        }

        // Sends notifications (if no other app is already deferring)
        //if ($shouldFlush) {
        //    $this->notificationManager->flush();
        //}

This is, afaict, exactly what the documentation says (Note that I stepped into the code and could see that $shouldFlush was true).

Well the defer is still within the same process.

You will have $shouldFlush if it's nested. This e.g. happens when the spreed app starts a call. We will defer the notifications until we finished them all and can then send the push notifications with only 1 request to the push proxy, instead of sending it to each user (in your code inactive user) with a new connection.

Actual behaviour

Nothing happens

What you are actually missing is $this->notificationManager->notify($notification); in your loop.
That is needed all the time, the defer/flush only delays the sending inside the notify method. It does not track createNotification

Argh! makes sense eventualy.

Sorry for the noise