0

I'm trying to write an app that will permanently listen to any incoming notification and then process them. I'm struggling at the first hurdle of making a notification listener that can run permanently even when the app is in the background. Any advice on where to start would be much appreciated?

Thanks

1 Answers1

-1

You should be using a service to achieve this.

A service that receives calls from the system when new notifications are posted or removed or their ranking changed.

You should have an android service class which implements NotificationListener()

class NotificationListenerService: NotificationListener() {
   override fun onBind(intent: Intent) {
      return super.onBind(intent)
   }

   override fun onNotificationPosted(sbn: StatusBarNotification) {
      // Implement what you want here
   }

   override fun onNotificationRemoved(sbn: StatusBarNotification) {
      // Implement what you want here
   }
}

Another required thing to do is to have it in your manifest file:

<service android:name=".NotificationListener"
      android:label="@string/service_name"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
 <intent-filter>
     <action android:name="android.service.notification.NotificationListenerService" />
 </intent-filter>

Hope it helps!

Refer for more details: https://developer.android.com/reference/android/service/notification/NotificationListenerService