Skip to main content

系统通知

推荐使用事件监听

通知监听已整合至事件监听模块, 推荐使用新的装饰器风格 API:

from ascript.android.event import NotificationEvent

@NotificationEvent.on(package="com.tencent.mm")
def on_wechat(event):
print(event.title, event.text)
# 模拟点击该通知
event.send()

详细文档请参阅 事件监听


旧版 API (仍可使用)

from ascript.android.system import Notification
warning

调用该方法,会触发权限请求.请在设备上开启监听权限.

您也可以在APP设置中,开启系统监听.

  • 函数
Notification.listen(tunner):
  • 参数
参数类型是否必填说明
tunnerfun(notiobj)python函数,必须带有1个形参
  • 示例
# 监听系统通知
from ascript.android.system import Notification
from android.app import Notification as AndroidNoti

def noti_callback(n):
print(n)
print("隶属包名",n.getPackageName())
print("标签",n.getTag())
print("ID",n.getId())
print("时间",n.getPostTime())
print("通知",n.getNotification())
# 获取通知对象
# 获取 通知-额外数据对象
android_noti = n.getNotification()
bundle = android_noti.extras
title = bundle.getString(AndroidNoti.EXTRA_TITLE)
print("标题",title)
text = bundle.getString(AndroidNoti.EXTRA_TEXT)
print("内容",text)
# 获取通知意图
intent = android_noti.contentIntent
print(intent)

# 跳转intent,模拟触摸该通知
intent.send()

Notification.listen(noti_callback)