有没有办法找出我的Android应用程序中的活动通知数量?

时间:2022-04-20 15:22:48

I am working towards managing the notifications that my app creates in the Android device. Is there any method that can provide me the number of notifications from my app that are active (i.e. still visible in the notification drawer) at any given time?

我正在努力管理我的应用在Android设备中创建的通知。是否有任何方法可以在任何给定时间向我提供来自我的应用程序的活动(即仍在通知抽屉中可见)的通知数量?

2 个解决方案

#1


You can use NotificationManager for getting list of all the active notifications posted by your application using getActiveNotifications()

您可以使用NotificationManager获取应用程序使用getActiveNotifications()发布的所有活动通知的列表

As per the reference doc for getActiveNotifications():

根据getActiveNotifications()的参考文档:

Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancelled by the app.

恢复活动通知列表:调用应用程序已发布但尚未被用户解雇或已被应用取消的通知列表。

#2


to expand on commonswares comment:

扩展commonswares评论:

I see two approaches here:

我在这里看到两种方法:

1) manage this number via a count sharedpreferences or a database, etc. you will need to supply a deleteIntent which starts something to update this number when they dismiss the notification and a contentIntent for when they open the notification (this will also update your count).

1)通过计数共享偏好或数据库等来管理这个号码。你需要提供一个deleteIntent,它在解除通知时启动更新这个号码的东西,并在打开通知时启动contentIntent(这也会更新你的点数) )。

2) read this number explicitly from a notification listener service.

2)从通知监听器服务中明确读取此数字。

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

#1 is much preferred because the user doesn't have to opt in to the behavior and asking for their full notification list is entirely unnecessary.

#1是首选,因为用户不必选择加入行为并且完全没有要求他们的完整通知列表。

#1


You can use NotificationManager for getting list of all the active notifications posted by your application using getActiveNotifications()

您可以使用NotificationManager获取应用程序使用getActiveNotifications()发布的所有活动通知的列表

As per the reference doc for getActiveNotifications():

根据getActiveNotifications()的参考文档:

Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancelled by the app.

恢复活动通知列表:调用应用程序已发布但尚未被用户解雇或已被应用取消的通知列表。

#2


to expand on commonswares comment:

扩展commonswares评论:

I see two approaches here:

我在这里看到两种方法:

1) manage this number via a count sharedpreferences or a database, etc. you will need to supply a deleteIntent which starts something to update this number when they dismiss the notification and a contentIntent for when they open the notification (this will also update your count).

1)通过计数共享偏好或数据库等来管理这个号码。你需要提供一个deleteIntent,它在解除通知时启动更新这个号码的东西,并在打开通知时启动contentIntent(这也会更新你的点数) )。

2) read this number explicitly from a notification listener service.

2)从通知监听器服务中明确读取此数字。

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

#1 is much preferred because the user doesn't have to opt in to the behavior and asking for their full notification list is entirely unnecessary.

#1是首选,因为用户不必选择加入行为并且完全没有要求他们的完整通知列表。