如何使用ADB shell清除通知

7
我知道我可以解锁屏幕、下拉通知栏并按清除通知按钮,但肯定有一种通过ADB清除通知的方法,对吧?我猜想这是通过“am”命令发送的某个意图,或者可能更简单,但我似乎在网上找不到任何信息。我只能得到用于apk的Java代码。
注:我应该提到我正在运行4.3,有时命令可能因版本而异。
2个回答

10

尝试:

adb shell service call notification 1

4
$ adb shell service call notification 1 结果:Parcel(fffffffc ffffffff '........')。翻译:使用ADB命令调用“notification”服务中的第一个方法,并返回结果,结果为一个字符包裹对象。 - MishaP
1
你需要root权限才能使用“service”命令。除此之外,在4.3版本中应该没有问题。 - Alex P.
谢谢,已接受的答案。我使用su -c而不是在root中重新启动设备:adb shell su -c service call notification 1 - MishaP
@MishaP:可能只是我一个人这样认为,但我认为您需要将“service call notification 1”用引号括起来才能使“su -c”正常工作。 - Corey Ogburn

6
如果您知道设备的类型和Android版本,即使没有root设备,也可以使用ADB清除通知。
思路是下拉通知并逐个滑动所有通知。
  1. Pull down:

    adb shell input swipe 0 0 0 300
    
  2. Swipe away:

    adb shell input swipe 0 400 300 400
    

需要注意的是,(x,y)坐标值因不同类型设备和Android版本而异。您需要进行多次检查来确定最适合您的x和y坐标。

完整脚本

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

更多详情请参见:https://www.sromku.com/blog/android-adb-clear-notifications

1
我已经尝试过了,但在我的情况下它并没有起作用,因为并非所有的通知都可以被解除,例如当第一次启动设备时可能会出现的Android向导通知。请参见:https://developer.android.com/reference/android/app/Notification.Builder#setOngoing(boolean) - Piotr Zawadzki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接