经过大量的研究、试验和错误。我得到了下面的解决方案!
PHP 代码将调用一个实际发送libnotify
消息的 shell 脚本。
bash 脚本:
#!/bin/bash
#
# This script shows how to send a libnotify message
# to a specific user.
#
# It looks for a process that was started by the user and is connected to dbus.
# process to determine DBUS_SESSION_BUS_ADDRESS
USER_DBUS_PROCESS_NAME="gconfd-2"
NOTIFY_SEND_BIN="/usr/bin/notify-send"
TITLE=$1
MESSAGE=$2
# get pid of user dbus process
DBUS_PID=`ps ax | grep gconfd-2 | grep -v grep | awk '{ print $1 }'`
# get DBUS_SESSION_BUS_ADDRESS variable
DBUS_SESSION=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/$DBUS_PID/environ | sed -e s/DBUS_SESSION_BUS_ADDRESS=//`
# echo $DBUS_SESSION;
# send notify
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION $NOTIFY_SEND_BIN "$TITLE" "$MESSAGE"
PHP代码:
$command = sprintf('/path/to/bash_script.sh "%s" "%s" 2> /dev/null', $title, $message);
system($command);
PS:这是在命令行应用程序中使用的,每当在 stackoverflow.com 上发布新问题时,它都会发送系统通知!(与您的标签匹配的问题)
你可以在这里查看:https ://kerneldev.com/2017/12/27/so-notify-a-stack-overflow-question-notifier/