Clear notifications using ADB
When running automated tests, it was important for me that there is no notifications of the app. There is no straight forward way for unrooted devices to clear all notifications from ADB, but there is a simple trick that will do the job.
The idea is to pull down
the notification bar, and swipe away
all
notifications. I can do this, because I choose the specific phone for my tests with knows Android
version, so I know what to expect.
-
Start from home:
adb shell input keyevent 3
-
Pull down the notification:
- Staying on
x=0px
- Swiping from
y=0px
toy=300px
:arrow_down:
adb shell input swipe 0 0 0 300
- Staying on
-
Clean one notification: After several checks, I found that in portrait mode, for Nexus 5x with Nougat, height y=400px will be in bounds of the first notification in the list.
- Swiping from
x=0px
tox=300px
:arrow_right: - Staying on
y=400px
adb shell input swipe 0 400 300 400
- Swiping from
Get the number of notifications
Just to clean all notifications that could appear while the phone was unused, I will count total number of notifications and do the step #3 same number of times.
adb shell dumpsys notification | grep NotificationRecord | wc -l
Final script
AAll together in one bash script: We start from home, pull and clean all notifications and finish at home.
#!/bin/bash
adb shell input keyevent 3
adb shell input swipe 0 0 0 300
num=$(adb shell dumpsys notification | grep NotificationRecord | wc -l)
echo $num
while [ $num -gt 0 ]; do
adb shell input swipe 0 400 300 400
num=$(( $num - 1 ))
done
adb shell input keyevent 3
That’s it. We have clean notification bar.