How to post the notification to be shown under the bell icon?

As an admin I would like to send manually the notifications to be shown under the bell icon on the apps panel. Only admin should be able to send the announcements.

So, I’ve installed announcementcenter app. Created a post there, however it is shown only in Activities tab.

Please, suggest, how to achieve my goal.

I use the cli for this.

USERS=(sudo -u www-data php occ ldap:search "" | rev | cut -d " " -f1 | rev | sed 's/(//g' | sed 's/)//g') )
for element in "${USERS[@]}"; do
eval 'sudo -u www-data php occ notification:generate' $element "'This is a notification for all users that will pop-up in any browser, be visible on device lock screens and will show under the notifications icon (the bell)'" $SILENT; done

This Piece of code lists all users (I am only interested in hitting users I get from my LDAP and not NC only users) and then sends the same notification to all those users.

The syntax is as follows for sending a notification for a single user:

sudo -u www-data php /var/www/nextcloud/occ notification:generate -l "Hi Long" 4ff8c864-1a6d-103a-8155-99f3e3d05420 "Hi short"

The userid is the GUID of the user. To get a list of users, use this:

sudo -u www-data php /var/www/nextcloud/occ user:list
2 Likes

This is great that you shared it :raised_hands:
For some, it might seem like a small thing, but when you’re dealing with automation or need to notify multiple users at once, having a concrete example like this can save a lot of time and effort.

It’s clear you put some thought into it, and it’s definitely helpful — even for me :+1:

Posts like this make a real difference and show the true value of the community.

1 Like

Oh, nice! Is it possible to send the message to a group as well?
Actually, I was looking for a way to do it via webUI…

Well not in one command. However you can use the same method to get a list of users in a group, and then in same way as above, iterate that list in a for loop.

occ group:list

To get all groups including members.
Unfortunately this is also the only CLI command that will show you members of the groups, and as such, you will need to do some work with your array manaipulation to get the group and members of your choosing. However when that is done, then yes, you can use that same for loop method I did above.

A little helpfull addition to your output, can be to output it as JSON:

sudo -u www-data php occ group:list --output=json

At least this way you can use json tricks to get the excact member list you look for.

The absolutely most effecient way to work with json nativaly in the CLI is to install jq.

USERS=(sudo -u www-data php /var/www/nextcloud/occ group:list --output=json | jq -r '.GROUPNAME')

This one tested and verified as getting the intended JSON items.

To get the full, functioning BASH code:

USERS=( $(sudo -u www-data php /var/www/nextcloud/occ group:list --output=json | jq -r '.MyGroup' | sed -e 's/\[//g' -e 's/\]//g' -e 's/\,//g' -e 's|["'\'']||g') )
for element in "${USERS[@]}"; do 
eval 'sudo -u www-data php occ notification:generate' $element "'This is a notification for all users that will pop-up in any browser, be visible on device lock screens and will show under the notifications icon (the bell)'" $SILENT; done
done

The above is tested and working.

1 Like

So this is an example of a fully working code where you can inout which group is in scope as a variable:

GROUP="MyGroup"
for element in $(sudo -u www-data php /var/www/nextcloud/occ group:list --output=json | jq -r ".$GROUP" | sed -e 's/\[//g' -e 's/\]//g' -e 's/\,//g' -e 's|["'\'']||g'); do
eval 'sudo -u www-data php occ notification:generate' $element "'This is a notification for all users that will pop-up in any browser, be visible on device lock screens and will show under the notifications icon (the bell)'" $SILENT;
done
3 Likes

If you sepcify as an admin that notfications should be sent as push messages they should show up from announcement center under the bell icon.

2 Likes

@SmallOne How to specify that and send on behalf of admin? Eg, I am an admin, I created the message in applicationcenter, and I do not see that message under the bell icon.

@Kerasit Can I ask you to adjust the code in order I can read the text from a file and place it in the notification, ideally formatted with paragraphs?


So the user must have push notifcations on, default is to only send email.

1 Like

That is trivial linux stuff and can be achieved by storing the file content in a variable or even used inline:

x=$(cat /path/to/file.txt)

Something like this:

SILENT="> /dev/null 2>&1"
GROUP="MyGroup"
for element in $(sudo -u www-data php /var/www/nextcloud/occ group:list --output=json | jq -r ".$GROUP" | sed -e 's/\[//g' -e 's/\]//g' -e 's/\,//g' -e 's|["'\'']||g') 
do sudo -u www-data php /var/www/nextcloud/occ notification:generate -l "$( cat ~/text.txt )" $element 'Short (Headline)' $SILENT; done

Just validated and working.

EDIT: I just forgot to add the $SILENT as it “sneaked” into the code I have been sharing. So here it is added.

The problem here is that this way it is not possible to send an individual notification, but only for a selected group.
Am I right?

Correct. This is only for group notifications. Individual ones has to be sent with the api or occ command

1 Like

@Kerasit I tried to go the same way, and in my case I’ve got errors, if the text value is a big text with paragraphs and URLS… I do not know whtether text formatting is supported in the notification at all.

@SmallOne Why not? Just use
sudo -u www-data php /var/www/nextcloud/occ notification:generate -l "text to send" <username> 'Short (Headline)'

via notification center you can only notify groups :slight_smile: I know how to do it with the api and occ command :slight_smile:

1 Like

Notifications is an app. If you findes the app and go to the GitHub repo of the app, you can find the documentation. There are a lot of limitations with the notification feature. First of all then the long text support maximum 4000 characters. The short, obviously way less.
You cannot use any kind of formatting. The text you enters are printed in a span object, så no breakline, Italic, bold, colors and whatever. Any character is literally printed as if escaped.

1 Like