В настоящее время у меня реализована часть перехвата, но открытие уведомление, которое должно быть вызвано pendingIntent.send(), не работает:
Код: Выделить всё
package com.example.notificationinterceptor
import android.app.PendingIntent
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
class NotificationInterceptor : NotificationListenerService() {
companion object {
const val TAG = "NotificationInterceptor"
const val TARGET_APP_PACKAGE = "com.example.notificationgenerator"
}
override fun onNotificationPosted(sbn: StatusBarNotification?) {
Log.d(TAG, "Got notification from ${sbn?.packageName}")
sbn?.let {
// Check if the notification comes from the desired app
if (sbn.packageName == TARGET_APP_PACKAGE) {
Log.d(TAG, "Notification from ${TARGET_APP_PACKAGE} detected")
// Get the notification's PendingIntent
val notification = sbn.notification
val pendingIntent = notification.contentIntent
// Try to trigger the PendingIntent, which simulates clicking the notification
try {
// **Here is where the notification is not opened**
pendingIntent.send()
} catch (e: PendingIntent.CanceledException) {
Log.e(TAG, "Failed to trigger notification: ${e.message}")
}
}
}
}
}
Что я делаю не так? Как я могу решить свою проблему? Я не хочу использовать приложения для автоматизации, такие как Tasker или аналогичные, мне нужно программное решение).
Подробнее здесь: https://stackoverflow.com/questions/790 ... listenerse