[notificationtool] Execute command when notification is tapped

I want to write a simple shell script that first sends out some info (possibly in a notification), then allows the user to toggle a switch or not, depending on the info.

I was thinking that it should be possible to execute a command when tapping a notification, but I can’t figure out how.

notificationtool has an Action option, which I suspect would allow me to do this:

-a, --action=ACTION        An action for the notification in
"ACTIONNAME[;DISPLAYNAME] DBUSSERVICE DBUSPATH DBUSINTERFACE METHOD [ARGUMENTS]..."  
format.

I have no idea how to implement any of this.
Can someone help?

You implement a DBus listener similar to this:

And then call the corresponding method on the bus.

Note that this can only call applications which are running and listening on the bus like in the link.

You can use tools like busctl --user tree to find which apps have which interfaces already listening.

For example, to trigger a Wifi network scan from connman:

$ busctl tree net.connman | tail
...
   └─/net/connman/technology
     ├─/net/connman/technology/bluetooth
     ├─/net/connman/technology/cellular
     ├─/net/connman/technology/gps
     └─/net/connman/technology/wifi

Introspect the path:

$ busctl introspect net.connman /net/connman/technology/wifi
NAME                                TYPE      SIGNATURE RESULT/VALUE FLAGS
net.connman.Technology              interface -         -            -
.GetProperties                      method    -         a{sv}        deprecated
.Scan                               method    -         -            -
.SetProperty                        method    sv        -            -
.PropertyChanged                    signal    sv        -            -
org.freedesktop.DBus.Introspectable interface -         -            -
.Introspect                         method    -         s            -

Now you get some interfaces and methods.

 $ busctl call net.connman /net/connman/technology/wifi net.connman.Technology Scan

Using the same method you can find out e.g. to open a new Tab in the Browser, you can:

busctl --user call org.sailfishos.browser.ui /ui org.sailfishos.browser.ui activateNewTabView
3 Likes

Thank you very much.

This is much harder than I thought; I’m no good with all this Dbus stuff.

All I have is a little shell script, launched via .desktop launcher, that reads some values and sends a notification. Ideally I’d like to tap the notification to toggle that value, i.e. execute the same shell script again.

Could you help me with formulating that (the .desktop launcher and the Action for the notification)?

This is all I have so far:

[Desktop Entry]
Type=Application
Name=Toggle Charging
Icon=
Exec=/home/nemo/.local/bin/charging-toggle
#Exec=invoker --type=generic --id=charging-toggle sh -c /home/nemo/.local/bin/charging-toggle
#Exec=sailjail -p charging-toggle.desktop /home/nemo/.local/bin/charging-toggle
#DBusActivatable=true

#X-Nemo-Application-Type=no-invoker
#X-Nemo-Application-Type=generic
#Custom-Launcher=yes
#X-Nemo-Single-Instance=no
#StartupNotify=false

[X-Sailjail]
Sandboxing=Disabled
#ExecDBus=invoker -T -F dummy.desktop --type=generic --id=charging-toggle sh -c /home/nemo/.local/bin/charging-toggle

Lots of commented stuff from some unrelated experiments.

And the script itself:

#!/bin/ash

me="${0##*/}"

# dependency checks
for dep in mcetool notificationtool; do
    command -V $dep >/dev/null || exit 1
done

if [ "$1" = toggle ]; then
    do something
    exit
fi

data="$(mcetool |grep -E ^'Charging|Battery level|Battery state')"
head="${data%%$'\n'*}"
data="${data#*$'\n'}"
notificationtool -o add -c x-nemo.battery -a "dosomething a a a exec charging-toggle toggle" "$head" "$data"

It’s not much.

This topic seems to be asking similar questions.