I am currently working on building a php command line app and I make use of notify-send
to send notification in my Ubuntu 17.10
which now comes with gnome.
The notifications look fine and as they should when I manually run the cli app in terminal. (at the top, in notification center)
but when the same app is run through a cron job the notificaiton looks completely different and is displayed in a different location! (top right)
I am calling the notify-send in my app using exec('notify-send tas)
The problem I have with the slightly different looking notification is I can not click on the hyper-links! while the other one supports clicking on hyper-links and then it opens up web page.
What is going wrong here?
After a lot of research, trial, and error. I arrived at the solution below!.
The PHP code will call a shell script which will actually send the libnotify
message.
#!/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"
$command = sprintf('/path/to/bash_script.sh "%s" "%s" 2> /dev/null', $title, $message);
system($command);
P.S: This was used in a command-line app that sends a system notification whenever a new question on stackoverflow.com is posted! (the questions that match your tags)
You can check it out here: https://kerneldev.com/2017/12/27/so-notify-a-stack-overflow-question-notifier/