2

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)

enter image description here

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)

enter image description here

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?

Community
  • 1
  • 1
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
  • I have not found the solution myself yet, but seems to be related to setting the correct DBUS_SESSION_BUS_ADDRESS env variable. – Davorin Mar 10 '20 at 22:00
  • I'm using bash and adding these 3 lines to my script worked, but I'm not sure if this is the correct answer. `PID=$(pgrep -n gnome-session)` `eval "export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ)"` `export DISPLAY=:0` – Davorin Mar 10 '20 at 22:27
  • 1
    @Davorin Hi there, I managed to solve this long back. Unfortunately I dont remember the exact solution but here is that project https://github.com/SapneshNaik/so-notify-A_Stack_Overflow_Question_Notifier please go through it if you're interested in knowing how I did it. – Sapnesh Naik Mar 11 '20 at 05:52
  • Thanks Sapnesh, looking at your bash code in `src/scripts/notify-send.sh` it seems to be a similar solution. You should post that code as the answer to your question, it could be valuable for the next person. – Davorin Mar 11 '20 at 13:19
  • 1
    @Davorin. Done!, Thanks for the suggestion – Sapnesh Naik Mar 11 '20 at 14:52

1 Answers1

1

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.

The bash script:


#!/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"

The PHP Code:

$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/

Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98