9

I would like to be able to send some kind of notification to my android using a remote shell script (SimpleSSHD running as root, so basically the same as an adb shell).

On an iOS device I can use the commands cvibrate to make the device vibrate and sbalert to display a notification via SSH. I would like to do the same, but on android.

It would be great if it could be an actual notification like any other app would invoke, but a way to just vibrate it would suffice aswell if both isn't possible on android.

This seems like a very simple task, but I've not been able to find a solution...

4
  • 2
    For Termux there is the Termux-vibrate and the Termux-notification package. Not sure how make those calls available in a stand-alone sshd. Also relevant: android.stackexchange.com/questions/207411
    – Robert
    Commented Feb 9, 2020 at 17:12
  • @Robert Looking at the source code of NotificationAPI.java and VibrateAPI.java - This is an app-based solution (that I'd like to avoid) that only works with Termux, using the Termux API app to control notifications/vibration/etc.
    – confetti
    Commented Feb 9, 2020 at 17:45
  • @Robert - HOWEVER: I've tried echo 200 > /sys/class/timed_output/vibrator/enable from the comments of the Q you linked: And it worked! I'd like to keep the qusetion open because I'd prefer notifications or some sort of alert/pop-up on the screen aswell, but if nothing else is possible without the Termux app + API that would be good enough of an answer for me. (Note for others: That command requires root (at least over SSH), which for me is not an issue though.)
    – confetti
    Commented Feb 9, 2020 at 17:46
  • Also see Notify4Scripts Commented Feb 9, 2020 at 17:49

2 Answers 2

9

Use cmd notification:

cmd notification post [flags] <tag> <text>

If you are root notification will not be displayed:

su -lp 2000 -c "cmd notification post -S bigtext -t 'Title' 'Tag' 'Multiline text'"
5
  • 1
    Nice. I was able to post notification with root. It still showed as being posted from Shell though. Android 10 on a OnePlus 6 using Termux.
    – Firelord
    Commented Dec 19, 2020 at 5:48
  • What is even more annoying for me: I found no way to send without triggering the notification sound
    – maxmopp
    Commented Dec 21, 2020 at 19:13
  • 2
    Which Android versions does/should this work on?
    – confetti
    Commented Dec 23, 2020 at 7:45
  • 2
    @confetti Android 10: android.googlesource.com/platform/frameworks/base/+/… Commented Jun 21, 2021 at 11:20
  • 2
    Simple example: adb shell cmd notification post -S bigtext -t 'Title' 'Tag' 'Multiline text' Commented Sep 20, 2022 at 17:21
6

You can setup a Task in Tasker which you can invoke from command-line. The task can be setup with actions of your liking in combination, such as show a notification and vibrate with XYZ pattern. When setting up a task in Tasker, setup the action like this: Alert → Notify

Fill up the fields as you like. Make sure to enable Allow External Access setting under Preferences → Misc, accessible from three-dots line on the right edge of top bar in Tasker's homescreen.

As for invoking the task from command line, run:

su -c am broadcast -a net.dinglisch.android.taskerm.ACTION_TASK --es "task_name" "YOUR_TASK"

Replace YOUR_TASK with your task's name.

Tip: you can setup Tasker to show text of your liking in your notification by a bit more tricks. Setup a task to watch for a file for changes and assign an action to change the value of a tasker variable to the contents of that file. In your aforesaid vibration/notification task, when filling up the field for "text", assign that local variable instead. So next time, when invoking the task, change the contents of the file for a custom text notification before sending that command.

Tip2: it is also possible that send variable directly to a task using command-line, but I don't have much experience with it so you've got to ask uncle web.

5
  • Thank you for the answer, +1 especially for the tips. I would much prefer a solution that doesn't require an external application. Seems not possible though, but I'll probably go with notify4scripts since it seems more lightweight and easier to customize.
    – confetti
    Commented Feb 9, 2020 at 20:26
  • 1
    @confetti every notification is sent by an app, so there isn't a way to create otherwise. System apps don't provide a method to send notification from commandline, so a 3rd party app is needed. I once tried to create some surfaces/windows directly dealing with SurfaceFlinger, but due to lack of knowledge couldn't figure out where to proceed. I don't think creating notifications natively without involving Android's Java framework would be a feasible solution (if possible at all). There is a mishmash of APIs and IPCs, at lower level closed source as well. Commented Feb 9, 2020 at 21:19
  • @IrfanLatif Thanks for the input. I guess I'll just stick with the vibrate-only solution then as it's the most simple and sufficient for my alert system. Maybe you want to add that to your answer so I can accept it? I'm sure the app-based solution will be helpful to others aswell.
    – confetti
    Commented Feb 9, 2020 at 21:24
  • @confetti yes the app based solution is helpful, I have been using it since long to send notifications from Linux environment. Notify4Scripts has a wiki page: github.com/halnovemila/Notify4Scripts/wiki and mentioned in an answer already: android.stackexchange.com/questions/155338/…. So I don't think it needs any further input. You may please self answer your question if you want to. Commented Feb 9, 2020 at 21:36
  • @IrfanLatif Oh, thank you, I was talking to Firelord though asking to add the echo 200 > /sys/class/timed_output/vibrator/enable command to the answer because both solutions are great and would be ideal for different scenarios and people so I feel like both deserve to be the accepted answer.
    – confetti
    Commented Feb 10, 2020 at 0:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .