Skip to content
HalEx edited this page Apr 4, 2020 · 25 revisions

Notify4Scripts

Sort of wrapper app that allows to call the notification service and build, trigger, customize notifications via shell commands.

As shell commands can be given to an Android device via adb and adb server can be connected to a device over WiFi network, Notify4Scripts allows to build and display notifications on a target phone, manually and programmatically from a remote computer.

NOTE: This document was written mainly at the time of the app development (last source code edit was 12 Dec 2014), while testing the app on a Samsung Galaxy Y, running Android Gingerbread 2.3.6
The app has been succesfully recently (by 5 Jun 2016) tested on two devices running Android Lollipop and KitKat, rooted with KingRoot.

Notify4Scripts has two service classes: NotifyService and NotifyServiceCV
The difference among the two is that NotifyServiceCV supports CustomView notifications and a number of parameters to customize the notification, while NotifyService only support standard basic notification.
The CustomView layout differs from the default layout in that the notification timestamp is placed on the same line of the notification title, that usually doesn't need all the length of the notification box, instead than on the same line of the notification content, which usually is longer and need as much space as possibile; Notify4Scripts CustomView layout is optimized and provide more space for the notification content than the default layout. Additional space can be gained by removing the notification time stamp as well as the icon.
CustomView layout makes also possible to use the \n newline escape sequence to split the text in multiple lines.
NotifyServiceCV will automatically use the CustomView notification layout if any parameter related to the notification appearance (size, colors, enable/disable timestamp/icon title) are passed to the service; otherwise the default standard notification layout will be used.
The app UI isn't related in any way to the main service and its features.

PARAMETERS

that can be passed to NotifyServiceCV are:

  • str_ticker Optional. text to be displayed on the notification ticker. If not set it will defaul to "Notify4Scripts ticker" and the notification sound will be played. If set to "" the ticker won't be displayed and no notification sound will be played
  • str_title Optional. text to be displayed as notification title. If not set it will default to "Notify4Scripts Notification Title". If set to "" CustomView layout will be used, the title line will be removed from the layout and the content text box will be expanded to fill all the available space.
  • str_content Optional. text to be displayed as notification content. If not set it will default to "Notify4Scripts Notification Content"
  • int_id Optional. number that identify the notification
  • hex_tcolor Optional. Color of title text. Format must be RGB Hex without hash (for example 85b8ff). CustomView layout will be used
  • hex_ccolor Optional. Color of content text. Format must be RGB Hex without hash (for example 85b8ff). CustomView layout will be used
  • float_tsize Optional. Size of title text in dpi. Format must be float (for example 18.5). CustomView layout will be used
  • float_csize Optional. Size of content text in dpi. Format must be float (for example 18.5). CustomView layout will be used
  • b_noicon Optional. If se to "1" no icon will be displayed inside the notification box. CustomView layout will be used
  • b_notime Optional. If se to "1" no time will be displayed inside the notification box. CustomView layout will be used
  • str_viewuri Optional. Set a URI as View intent of the notification action. By touching the notification the default system action associated to the type of the target URI will be executed.
  • str_exec Optional. Set the commands that will be executed by touching the notification. See examples below
  • b_execdebug Optional. If set to "1" debug messages will be sent to Log service.
  • str_staticonuri NOT WORKING YET. A URI to a drawable resource that will be used as notification icon on status bar
  • str_iconuri NOT WORKING YET. A URI to a drawable resource that will be used as notification icon on notification box
  • int_iconid Optional. Set the notification icon based on the id number of the available drawable icon resources in the app package. Id must be a integer number on the range 0 to 6 as there are 7 drawable available. If an invalid value is given, Notify4Script will use the default value of 0.
  • b_autocancel Optional. If set to "1" the notification will have the AutoCancel flag set to "true"; that means that the notification will be automatically cancelled/removed/cleared once it's touched by the user. Default is "false"
  • int_cancel Optional. The Id of the notification that will be "cancelled/removed/cleared". Id must be a integer number. If an invalid value is given, Notify4Script will use the default value of 0. When int_cancel parameter is passed to Notify4Scripts, no matter if the given value is or isn't valid, THE ONLY ACTION PERFORMED will be the cancellation of the notification.

EXAMPLES OF USAGE

NOTE: Whereas while testing the app on phones running KitKat and Lollipop no issues have been observed related to the making of the notifications, execution of notification actions that calls the system shell (/system/bin/sh) may not work. Eventually some additional permissions use have to be declared on the app Manifest.
Also make sure that the app is granted root privileges in order to execute actions that make use of "su" command.

The following examples can be executed directly on the phone device using a Terminal Emulator console or shell script file, or they can be executed remotely using "adb shell" and connecting the computer adb server to the phone adb over WiFi network (see here how http://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp).

I found out that it's very difficult, if not even impossible in some cases, to find informations on which parameters to pass to ActivityManager "am" command in order to have an Android app launched and performing some task as if it is launched by home screen icon.
The best source I found is actually the system launcher database that is located in the data folder of the launcher app.
For example in the case of a stock Samsung Galaxy Y the database is located in /data/data/com.sec.android.app.twlauncer/databases/launcher.db
I've been able to view the database using Root Explorer app embedded SQLite database viewer

Example of activity that start the execution of one playlist (same as the home screen music playlist shortcut) .The str_exec string begin with a "/system/bin/sh -" command followed by a newline (same a /system/bin/sh -c") because the "am" (Activity Manager) command is part of the Android shell command interpreter and not a separate executable.
am startservice -e str_exec "/system/bin/sh - \n am start -a android.intent.action.VIEW -t vnd.android.cursor.dir/playlist -e playlist 1 -n com.android.music/.PlaylistBrowserActivity" -e int_id 3 -e str_title "Start Music" -e hex_tcolor "FFaaEE" -e float_tsize 27 -e float_csize 16 -n com.hal9k.notify4scripts/.NotifyServiceCV

Example of notification activity that "unfreeze" an app. Option b_execdebug has been enabled so some debug messages will be sent to Log service
am startservice -e str_exec "su -c \"pm enable com.google.android.apps.maps\"" -e int_id 2 -e str_title "UnFreeze Maps" -e b_execdebug 1 -n com.hal9k.notify4scripts/.NotifyServiceCV

Example of notification activity that "freeze" an app
am startservice -e str_exec "su -c \"pm disable com.google.android.apps.maps\"" -e int_id 1 -e str_title "Freeze Maps" -e b_execdebug 1 -n com.hal9k.notify4scripts/.NotifyServiceCV

Example of notification that launches an app. You can use the shell command pm list packages -d to get a list of the installed apps and their names.
am startservice -e str_exec "am start -a android.intent.action.MAIN -n com.yahoo.mobile.client.android.im/.YahooMessenger" -e int_id 4 -e str_title "Start YM" -e hex_tcolor "CCaaFF" -e float_tsize 27 -e float_csize 16 -n com.hal9k.notify4scripts/.NotifyServiceCV

Terminal Emulator (by Jackpal) is invoked as it would if launched from a Terminal Emulator home screen shortcut. The first command that have to be executed by the NotifyService is the shell as the "am" (Activity Manager) command is part of the Android shell command interpreter and not a separate executable. shell can be invoked either with "/system/bin/sh -" for a login Shell or with "/system/bin/sh" for a Non-login Shell. Commands output redirection works as usual in true shell
NOTE: as the command string passed to su is nested into the string passed to Terminal Emulator activity that is nested into string passed to Notify4Service there have been the need to use two different ways to escape the double quote character " that sets the start and end of the strings:
the 1st level nested string has been quoted with use of escaped double quote character "
the 2nd level nested string has been quoted with use of URL encoding (as the data passed to Terminal Emulator is actually a URI)
%22 is equivalent to "
am startservice -e str_exec "/system/bin/sh \n am start -a jackpal.androidterm.RUN_SCRIPT -d \"file:#echo; echo Working directory is: ;pwd ; echo; sleep 3 ; echo Hello World! ; echo; sleep 3 ; echo su -c %22busybox ps | grep notify4scripts%22 ; su -c %22busybox ps | grep notify4scripts%22; echo; sleep 5; su -c %22echo This file is been created by Notify4Script NotifyService by invoking a echo output redirection from a Terminal Emulator window > $EXTERNAL_STORAGE/dummyFile.txt%22 && echo && echo Created $EXTERNAL_STORAGE/dummyFile.txt with text content; echo; sleep 3; echo Closing this Terminal window in 5s... ; echo; sleep 3; echo Bye! :^\) ; echo; sleep 2; exit\" -t text/plain -n jackpal.androidterm/.RemoteInterface" -e int_id 4 -e str_title "Hello World!" -e hex_tcolor "000000" -e float_csize 16 -e str_content "Start Terminal Emulator and run few commands with output redirection" -n com.hal9k.notify4scripts/.NotifyServiceCV

Execute a shell command with su privileges; the command is passed to su at the time su is invoked. Use of "su -c [command]" might not work if [command] contain spaces because [command] should be a single parameter, and thus will require quoting. Unfortunately both quoting the [command] parameter as well as passing the paramaters as separate variables does not work consistently across all Android versions. Commands output redirection isn't possible as the output stream isn't handled by a true shell but is sent to the process's parent that is the NotifyService
am startservice -e str_exec "su -c \"touch $EXTERNAL_STORAGE/dummyFile.txt\"" -e int_id 6 -e str_title "Make a file" -e hex_tcolor "000000" -e float_csize 16 -e str_content "Create a new empty file in /mnt/sdcard with name dummyFile.txt" -n com.hal9k.notify4scripts/.NotifyServiceCV

Same as the above example, but in this case the shell command is passed to su after su is been invoked .Commands output redirection isn't possible as the output stream isn't handled by a true shell but is sent to the process's parent that is the NotifyService
am startservice -e str_exec "su \ntouch $EXTERNAL_STORAGE/dummyFile.txt \ntouch /mnt/sdcard/dummyFile2.txt" -e int_id 6 -e str_title "Make a file" -e hex_tcolor "000000" -e float_csize 16 -e str_content "Create a new empty file in /mnt/sdcard with name dummyFile.txt" -e b_execdebug 1 -n com.hal9k.notify4scripts/.NotifyServiceCV

Execute multiple shell commands separated by a new line marker, and collect the output that is then sent to the Log Cat service.
Note that when CustomView is enabled (because of any parameter related to the notification appearance passed to the service) it's possible to use the \n newline escape sequence to split the text of both title and content in multiple lines.
am startservice -e str_exec "su \npm enable com.google.android.apps.maps \necho Google Maps Unfreezed!\n echo Task completed succesfully\necho Have a nice day!" -e int_id 6 -e str_title "Unfreeze Google Maps\nand echo something" -e hex_tcolor "000000" -e float_csize 16 -e str_content "Test for logging of stderr and stdout streams" -e b_execdebug 1 -n com.hal9k.notify4scripts/.NotifyServiceCV

Change the wallpaper theme to Hyperion
am startservice -e str_exec "su \ncp -f $EXTERNAL_STORAGE/Pictures/Wallpapers/HyperionGM9_wallpaper_\(copyTo.data_com.android.settings_files\).jpg /data/data/com.android.settings/files/wallpaper ; chmod 770 /data/data/com.android.settings/files/wallpaper ; chown 1000:1000 /data/data/com.android.settings/files/wallpaper ; cp -f $EXTERNAL_STORAGE/Pictures/Wallpapers/HyperionGM9_zzzzzz_lockscreen_wallpaper_\(copyTo.data_com.cooliris.media_files\).jpg /data/data/com.cooliris.media/files/zzzzzz_lockscreen_wallpaper.jpg ; chmod 664 /data/data/com.cooliris.media/files/zzzzzz_lockscreen_wallpaper.jpg ; chown 1000:1000 /data/data/com.cooliris.media/files/zzzzzz_lockscreen_wallpaper.jpg" -n com.hal9k.notify4scripts/.NotifyServiceCV -e str_title "Hyperion Wallpaper" -e str_content "Touch here to set the current home and lockscreen wallpaper to Hyperion theme" -e hex_tcolor "033874" -e float_tsize 20 -e int_iconid 1 -e int_id 1

Change the wallpaper theme to Aurora
am startservice -e str_exec "su \ncp -f $EXTERNAL_STORAGE/Pictures/Wallpapers/InfectedAurora_wallpaper_\(copyTo.data_com.android.settings_files\).jpg /data/data/com.android.settings/files/wallpaper ; chmod 770 /data/data/com.android.settings/files/wallpaper ; chown 1000:1000 /data/data/com.android.settings/files/wallpaper ; cp -f $EXTERNAL_STORAGE/Pictures/Wallpapers/InfectedAurora_zzzzzz_lockscreen_wallpaper_\(copyTo.data_com.cooliris.media_files\).jpg /data/data/com.cooliris.media/files/zzzzzz_lockscreen_wallpaper.jpg ; chmod 664 /data/data/com.cooliris.media/files/zzzzzz_lockscreen_wallpaper.jpg ; chown 1000:1000 /data/data/com.cooliris.media/files/zzzzzz_lockscreen_wallpaper.jpg" -n com.hal9k.notify4scripts/.NotifyServiceCV -e str_title "Aurora Wallpaper" -e str_content "Touch here to set the current home and lockscreen wallpaper to Aurora theme" -e hex_tcolor "042c15" -e float_tsize 20 -e int_iconid 2 -e int_id 2

Send a sms
am startservice -e str_exec "su \nservice call isms 6 s16 \"222\" i32 0 i32 0 s16 \"bal\"" -n com.hal9k.notify4scripts/.NotifyServiceCV -e str_title "Bal Inquiry" -e str_content "By touching here a SMS with text \"bal\" will be sent to 222. You'll receive a reply SMS" -e hex_tcolor "94ceef" -e b_noicon 1 -e str_ticker "" -e int_iconid 2 -e int_id 3

Run a shell script with arguments Use of "su -c [command]" instead of "su\n[command]" doesn't work (or it likely might not work) because [command] should be a single parameter, and thus may require quoting.
Unfortunately both quoting the [command] parameter as well as passing the paramaters as separate variables does not work consistently across all Android versions.
am startservice -e str_exec "su\n/data/scripts_hal9k/keypad_switch.sh -no_notification_update" -e int_id 4 -e str_content "Touch here to switch keypad type" -e hex_ccolor "85c8ff" -e float_csize 19 -e str_title " " -e float_tsize 12 -e b_notime 1 -e b_noicon 1 -e int_iconid 4 -n com.hal9k.notify4scripts/.NotifyServiceCV

Launching Terminal Emulator and make it start a shell script (dynamic notification content update). Having the Terminal Emulator to execute a shell script, instead of having the script executed directly from the notification, allows for automated notification content update. An existing notification can be updated by use of notify4scripts service but said service can't be invoked as action from an existing notification because it's the notify4scripts service the one that handles the existing notification and will make the call, so it will end up in a loop, having the service invoking itself and that's not allowed. But if the notification launches Terminal Emulator, as it will run on its own indipendent task, the notify4scripts service can be invoked again from commands or scripts launched within Terminal Emulator. NOTE: "su -c [command]" might not work if [command] contain spaces because [command] should be a single parameter, and thus will require quoting. Unfortunately both quoting the [command] parameter as well as passing the paramaters as separate variables does not work consistently across all Android versions.
am startservice -e str_exec "/system/bin/sh \n am start -a jackpal.androidterm.RUN_SCRIPT -d \"file:#su -c /data/scripts_hal9k/keypad_switch.sh ; exit\" -t text/plain -n jackpal.androidterm/.RemoteInterface" -e int_id 4 -e str_title "Touch here to switch keypad type" -e hex_tcolor "85b8ff" -e float_tsize 18 -e float_csize 16 -e str_content "Last keypad type setting was $last_set_keypad" -e b_notime 1 -e b_noicon 1 -e int_iconid 4 -n com.hal9k.notify4scripts/.NotifyServiceCV;

Clone this wiki locally