可以通过DBus触发和调用org.freedesktop.Notifications.CloseNotification(uint id)吗?

参考:
关于notify-osd的关闭按钮?

书签:
可以通过DBus触发和调用org.freedesktop.Notifications.CloseNotification(uint id)吗?

目前,这个脚本

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

将杀死所有待处理的通知。

最好通过使用org.freedesktop.Notifications.CloseNotification(uint id)org.freedesktop.Notifications.CloseNotification(uint id)要取消的特定目标OSD通知。 是否有一个接口方法可以在特定的通知事件发生时将其置于(在?)DBus中?

该方法需要获取通知PID以用作CloseNotification(uint id)的参数。 或者,

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

如果可以确定(uint id)参数,则可以从shell中使用。 实际的命令语法将使用整数代替(uint id)

或许首先要问的一个更好的问题可能是“如何找到通知的DBus地址?”。
这些都没有正确识别它:

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

回报

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

要么

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

回报

 ... string ":1.340" ... 

事后看来,前一个问题“发现通知的(uint id)如何?”鉴于此前对另一个问题的回答是如此:
https://askubuntu.com/a/186311/89468
提供详细信息,因此可以使用以下任一方法:

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

返回:

 (uint32 8957,) 

要么

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

返回:

 8957 

弹出通知-OSD graffit将被击败:

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

“诀窍”是找到:?.??? DBus地址。

你怎么知道什么(UINT32 id)是或不是?

鉴于声明You ... need a way to get that ID ...在这个答案和以下经验观察中,问题的标题可能应该改为“ gdbus ... -m org.freedesktop.Notifications.CloseNotification ...如何gdbus ... -m org.freedesktop.Notifications.CloseNotification ...关闭通知?“

是否通过暗示或消除过程,声明“……它只声明ID是唯一的,非零的,并且小于MAXINT ……”,见9.1.2的描述。 org.freedesktop.Notifications.Notify ,也适用于CloseNotification参数(UINT32 id)UINT32通过消除过程是文档中作为属性类型的唯一常见因素,并且暗示必须存在某种常见ID“句柄”但是……

Notify的返回值及其参数UINT32 replaces_id的详细信息非常明确,但不清楚CloseNotification(UINT32 id)是否与此有关,如下面的经验测试所示。

引用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. 

这并不表征或解释(UINT32 id)的含义。

一个明显的遗漏是缺少一个描述NotificationClosed (UINT32 id)参数的表。 方法和信号的所有其他参数都使用此类表进行限定。

经验测试显示通知不会使用Notify的返回值关闭:

  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' ) 

要么

  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' ) 

如果(UINT32 id)Notify的返回值标识,则通知不会关闭。

更多测试:

  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' ) 

甚至强制一个常量ID,通过将UINT32 replaces_id设置为42并将expire_timeout为0,可以看出CloseNotification没有影响

  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' ) 

即使42在整个方法中进行往返,通知也不会关闭

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

在输出中

方法调用sender =:1.332  - > dest = org.freedesktop.Notifications serial = 3 path = / org / freedesktop / Notifications; 接口= org.freedesktop.Notifications; 构件=通知
   字符串“”
    uint32 42
   字符串“”
   字符串“sigma-ry”
   字符串“corpus”
   arrays[
    ]
   arrays[
    ]
    int32 0
方法调用sender =:1.333  - > dest = org.freedesktop.Notifications serial = 3 path = / org / freedesktop / Notifications; 接口= org.freedesktop.Notifications; 构件= CloseNotification
    uint32 42

还要注意-1超时的通知失败(文档是矛盾的 ):

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

虽然这“有效”

 notify-send "test" -t -1 

参考:
显示通知的程序名称是什么? (文件矛盾)
如何在notify-osd中强制新的通知显示而不等待先前的退出?
如何使用’notify-send’立即替换现有通知?

书签:
可以通过DBus触发和调用org.freedesktop.Notifications.CloseNotification(uint id)吗?

CloseNotification方法被规范定义为D-Bus方法。 但是,它仅声明ID是唯一的,非零且小于MAXINT。 它不要求ID是调用进程的PID。 在成功调用方法之前,您需要一种获取该ID的方法。

我知道我有点迟了,但我今天自己也遇到了这个问题,这可能对其他人有用!

首先,安装一个修补的libnotify-bin ,它支持-p选项以打印通知ID:

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

现在是一个示例脚本,它发送一个没有超时的通知,然后在某个命令完成后关闭通知(在这种情况下,只需要睡5秒):

 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)