Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

ref:
Close button on notify-osd?

Bookmark:
Can org.freedesktop.Notifications.CloseNotification(uint id) be triggered and invoked via DBus?

Currently, this script

dbus-monitor "interface='org.freedesktop.Notifications'" |             \
grep --line-buffered "member=Notify"                     |             \
sed -u -e  's/.*/killall notify-osd/g'                   |             \
bash

will kill all pending notifications.

It would be better to finesse the specific target OSD notification to cancel, by using org.freedesktop.Notifications.CloseNotification(uint id). Is there an interface method that can put this on (in?) the DBus to fire when a particular notify event occurs?

The method will need to get the notify PID to use as the argument for CloseNotification(uint id). Alternatively,

  qdbus    org.freedesktop.Notifications                \
          /org/freedesktop/Notifications                \
           org.freedesktop.Notifications.CloseNotification(uint id)

could be used from the shell, if the (uint id) argument could be determined. The actual command syntax would use an integer in place of (uint id).

Perhaps a better question to ask first might be "How is a notification's DBus address found?".
Neither of these identify it correctly:

gdbus monitor --session --dest org.freedesktop.Notifications

returns

The name org.freedesktop.Notifications is owned by :1.130
...

or

dbus-monitor "interface='org.freedesktop.Notifications'"

returns

... string ":1.340" ...

In hindsight, the previous question "How is the (uint id) for a notification found?"is rhe torical given this previous answer to another question:
http://askubuntu.com/a/186311/89468
provides details so either method below can be used:

gdbus call --session --dest org.freedesktop.DBus                                   \
             --object-path /                                                       \
                   --method org.freedesktop.DBus.GetConnectionUnixProcessID :1.16

returning:

(uint32 8957,)

or

qdbus --literal --session  org.freedesktop.DBus                                  \
                          /                                                      \
                           org.freedesktop.DBus.GetConnectionUnixProcessID :1.16

returning:

8957

The pop-up notify-OSD graffit would be then defeated with:

  qdbus    org.freedesktop.Notifications                             \
          /org/freedesktop/Notifications                             \
           org.freedesktop.Notifications.CloseNotification           \
  $(qdbus --literal --session                                        \
           org.freedesktop.DBus                                      \
          /                                                          \
           org.freedesktop.DBus.GetConnectionUnixProcessID :?.???  )

The "trick" is to find the :?.??? DBus address.

share|improve this question
add comment

3 Answers

How do you know what (UINT32 id) is or is not?

Given the statement You ... need a way to get that ID ... in this answer and the following empirical observations perhaps the title of the question should be changed to "How can gdbus ... -m org.freedesktop.Notifications.CloseNotification ... close a notification?"

Is it by implication or process of elimination that the statement "... it only states that the ID is to be unique, non-zero, and less than MAXINT ...", found in the description of 9.1.2. org.freedesktop.Notifications.Notify, also applies to the CloseNotification parameter (UINT32 id)? The UINT32 by process of elimination is the only common factor as an attribute type in the documentation and there is the implication that there must be some sort of common ID "handle" but ...

The details of the returned value of Notify and its parameter UINT32 replaces_id are quite explicit but it is not clear that CloseNotification's (UINT32 id) has anything to do with that as demonstrated with empirical testing below.

Quoting 9.1.3. org.freedesktop.Notifications.CloseNotification in toto:

9.1.3. org.freedesktop.Notifications.CloseNotification

void org.freedesktop.Notifications.CloseNotification (UINT32 id);

Causes a notification to be forcefully closed and removed from the user's view.
It can be used, for example, in the event that what the notification pertains to
is no longer relevant, or to cancel a notification with no expiration time.

The NotificationClosed signal is emitted by this method.

If the notification no longer exists, an empty D-BUS Error message is sent back.

This does not characterize or explain the meaning of (UINT32 id).

A glaring omission is the lack of a table to describe the NotificationClosed (UINT32 id) parameter. All the other parameters for methods and signals are qualified with such tables.

Empirical tests show notifications do not close using Notify's return value:

 qdbus  org.freedesktop.Notifications                           \
       /org/freedesktop/Notifications                           \
        org.freedesktop.Notifications.CloseNotification         \
$(gdbus call --session                                          \
     -d org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
     -m org.freedesktop.Notifications.Notify                    \
                     "" 0 "" "sigma-ry" "corpus" [] {} 200      \
 | sed -e 's/(uint32\(.*\),)/\1/g'                            )

or

 qdbus  org.freedesktop.Notifications                           \
       /org/freedesktop/Notifications                           \
        org.freedesktop.Notifications.CloseNotification         \
$(gdbus call --session                                          \
    -d  org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
    -m  org.freedesktop.Notifications.Notify                    \
                     "" 5 "" "sigma-ry" "corpus" [] {}  20      \
 | sed -e 's/(uint32\(.*\),)/\1/g'                            )

The notification does not close which would be the case if (UINT32 id) identified with the returned value of Notify.

Some more tests:

 gdbus call --session                                           \
    -d  org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
    -m  org.freedesktop.Notifications.CloseNotification         \
$(gdbus call --session                                          \
    -d  org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
    -m  org.freedesktop.Notifications.Notify                    \
                     "" 0 "" "sigma-ry" "corpus" [] {}     0    \
 | sed -e 's/(uint32\(.*\),)/\1/g'                            )

and even coercing a constant ID, by setting UINT32 replaces_id to 42 and expire_timeout to 0, it is seen that CloseNotification has no influence

 gdbus call --session                                           \
    -d  org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
    -m  org.freedesktop.Notifications.CloseNotification         \
$(gdbus call --session                                          \
    -d  org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
    -m  org.freedesktop.Notifications.Notify                    \
                     "" 42 "" "sigma-ry" "corpus" [] {}    0    \
 | sed -e 's/(uint32\(.*\),)/\1/g'                            )

The notification does not close even though the 42 makes the round trip throughout the methods as confirmed by

dbus-monitor "interface='org.freedesktop.Notifications'"

in the output

method call sender=:1.332 -> dest=org.freedesktop.Notifications serial=3 path=/org/freedesktop/Notifications; interface=org.freedesktop.Notifications; member=Notify
   string ""
   uint32 42
   string ""
   string "sigma-ry"
   string "corpus"
   array [
   ]
   array [
   ]
   int32 0
method call sender=:1.333 -> dest=org.freedesktop.Notifications serial=3 path=/org/freedesktop/Notifications; interface=org.freedesktop.Notifications; member=CloseNotification
   uint32 42

Also note the notification failure of a -1 timeout (documentation is contradictory):

  gdbus call --session                                          \
    -d  org.freedesktop.Notifications                           \
    -o /org/freedesktop/Notifications                           \
    -m  org.freedesktop.Notifications.Notify                    \
                     "" 42 "" "sigma-ry" "corpus" [] {}   -1

though this "works"

notify-send "test" -t -1

ref:
What is the name of the program that displays the notifications? (document contradictions)
How to force a new Notification in notify-osd to show up without waiting for the earlier one to exit?
How do I use 'notify-send' to immediately replace an existing notification?

Bookmark:
Can org.freedesktop.Notifications.CloseNotification(uint id) be triggered and invoked via DBus?

share|improve this answer
add comment

The CloseNotification method is defined as a D-Bus method by the specification. However, it only states that the ID is to be unique, non-zero, and less than MAXINT. It does not require that the ID be the PID of the calling process. You would need a way to get that ID before you could call the method successfully.

share|improve this answer
add comment

I know I'm bit late, but I faced this problem myself today, and this may be useful to others!

First of all, install a patched libnotify-bin that supports the option -p to print the notification id:

sudo apt-add-repository ppa:izx/askubuntu
sudo apt-get update
sudo apt-get install libnotify-bin

Now an example script that sends a notification without timeout, and then closes the notification after a certain command is finished (in this case, just sleep 5 seconds):

notify-send 'Message...' -i dialog-information \
    -t 0 -h int:transient:1 \
    -p >/tmp/notification_id

sleep 5

qdbus org.freedesktop.Notifications \
     /org/freedesktop/Notifications \
      org.freedesktop.Notifications.CloseNotification \
    $(cat /tmp/notification_id)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.