列出Android中的独特通知
我正在创建一个将有权阅读用户通知的应用程序。 因此,我已经实施了如下所示的NotificationListenerService
class NotifListener : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
var context = this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
var packageName = sbn?.packageName ?: ""
var notifId=sbn?.id?:0; //doesn't seem to be unique
val notification: Notification = sbn!!.notification
var extras: Bundle? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
extras = notification.extras
for (key in extras!!.keySet()) {
var title: String = extras.get("android.title").toString()
var text: String = extras.get("android.text").toString()
CoroutineScope(Dispatchers.IO).launch {
NotificationDBBuilder.getInstance(context).notificationDao()
.insertNotification(
NotificationEntity(notifId, title, text,packageName,Date().time)
)
}
}
}
extras?.get("android.title")
// Log.d(TAG, "onNotificationPosted: " + extras?.get("android.title")+" "+extras?.get("android.text")+" "+extras?.get("android.infoText"))
}
}
}
,并且现在在清单中添加了它,
<service android:name=".services.NotifListener"
android:label="Notif"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
当我记录收到的通知时,似乎在某些应用程序或某些情况下多次将OnontificaitificAityPosted称为多次。我想仅列出唯一的通知。我应该从通知中使用哪个参数作为唯一键? 还是有某种方法可以防止多个通知通知? 还是听众多次注册?
I am creating an app that will have permission to read user notifications.
So I have implemented NotificationListenerService as shown below
class NotifListener : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
var context = this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
var packageName = sbn?.packageName ?: ""
var notifId=sbn?.id?:0; //doesn't seem to be unique
val notification: Notification = sbn!!.notification
var extras: Bundle? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
extras = notification.extras
for (key in extras!!.keySet()) {
var title: String = extras.get("android.title").toString()
var text: String = extras.get("android.text").toString()
CoroutineScope(Dispatchers.IO).launch {
NotificationDBBuilder.getInstance(context).notificationDao()
.insertNotification(
NotificationEntity(notifId, title, text,packageName,Date().time)
)
}
}
}
extras?.get("android.title")
// Log.d(TAG, "onNotificationPosted: " + extras?.get("android.title")+" "+extras?.get("android.text")+" "+extras?.get("android.infoText"))
}
}
}
and I have added it in manifest
<service android:name=".services.NotifListener"
android:label="Notif"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Now when I logged the notifications that I received, it seems that the onNotificaitionPosted is being called multiple times for the same notification for some applications or some situations. I want to list only Unique notifications. Which parameter should I Use from the notifications as a Unique Key?
Or is there some way to prevent multiple calls for the same notification?
or is it possible that the listener is getting registered multiple times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论